当前位置: 首页 > news >正文

北京建设网站的公司哪家好沈阳今天刚刚发生的新闻

北京建设网站的公司哪家好,沈阳今天刚刚发生的新闻,品牌建设内容,wordpress 中文标签插件下载Java 8引入了一种新的抽象概念——流(Stream),它允许我们以声明式方式处理数据集合。通过使用流,可以更简洁、更易读地编写代码来对集合进行复杂的操作,如过滤、映射、排序等。本文将介绍Java 8的流操作的基本概念和使…

Java 8引入了一种新的抽象概念——流(Stream),它允许我们以声明式方式处理数据集合。通过使用流,可以更简洁、更易读地编写代码来对集合进行复杂的操作,如过滤、映射、排序等。本文将介绍Java 8的流操作的基本概念和使用方法。

 

创建流

要使用流,首先需要创建一个流。流可以从多种数据源创建,如数组、集合、文件等。以下是一些创建流的示例:

// 从数组创建流
int[] numbers = {1, 2, 3, 4, 5};
IntStream intStream = Arrays.stream(numbers);// 从集合创建流
List<String> names = Arrays.asList("张三", "李四", "王五");
Stream<String> nameStream = names.stream();// 从文件创建流
try (Stream<String> lines = Files.lines(Paths.get("example.txt"))) {lines.forEach(line -> {/*TODO:逐行处理文件内容*/ });
} catch (IOException e) {e.printStackTrace();
}

 

中间操作

中间操作是对流中的元素进行某种处理的操作,例如过滤、映射、排序等。中间操作不会改变流本身,而是返回一个新的流,因此可以通过链式调用方法组合多个中间操作。以下是一些常见的中间操作:

// 过滤元素
IntStream evenNumbers = intStream.filter(n -> n % 2 == 0);// 转换元素
Stream<Integer> evenNumbers1 = evenNumbers.mapToObj(Integer::new);// 映射元素
Stream<String> upperCaseNames = nameStream.map(String::toUpperCase);// 排序元素
Stream<String> sortedNames = nameStream.sorted();
Stream<String> revSortedNames = names.stream().sorted(Comparator.reverseOrder());

 

终端操作

终端操作是对流进行最终处理的操作,例如计算结果、遍历元素等。终端操作会消耗流,即执行后流将不再可用。以下是一些常见的终端操作:

// 计算结果(如求和、平均值等)
// 注意:实际使用时,一个流只能使用一次,再次使用需要重新生成流,否则会收到下列异常:
// java.lang.IllegalStateException: stream has already been operated upon or closed
double average = intStream.average().orElse(0);
int sum = intStream.sum();
long count = intStream.count();
OptionalInt max = intStream.max();
OptionalInt min = intStream.min();// 遍历输出每个元素
intStream.forEach(System.out::println);// 收集到列表中
List<Integer> numbersList = intStream.collect(Collectors.toList());

 

并行流

Java 8还提供了并行流(Parallel Stream),它可以充分利用多核处理器的优势,提高处理速度。要创建一个并行流,只需在创建流时调用parallel()方法或者使用parallelStream()创建流即可:

// 创建一个并行流
IntStream parallelIntStream = intStream.parallel();
Arrays.asList("张三", "李四", "王五").parallelStream();

需要注意的是,并非所有情况下都适合使用并行流。在某些情况下,由于线程切换的开销,并行流的性能可能不如串行流。因此,在使用并行流时,需要根据具体情况进行权衡。
 

 

示例:产品分组

使用流可以方便地处理数据,如过滤列表中库存大于0的产品,然后按产品类型进行分组。以下为简单的代码示例,其中省去了代码运行非必需的getter()和toString()方法:

public static void main(String[] args) {List<Product> list = new ArrayList(){{add(new Product(1, 1, 0, "prod1"));add(new Product(2, 1, 5, "prod2"));add(new Product(3, 2, 10, "prod3"));add(new Product(4, 2, 11, "prod4"));add(new Product(5, 2, 0, "prod5"));}};Map<Integer, List<Product>> map = list.parallelStream() // 创建并行流.filter(product -> product.getInventory() > 0) // 过滤库存大于0的产品.collect(Collectors.groupingBy(Product::getProductType)); //分组后收集到Map中System.out.println(map);
}public static class Product {int productId;int productType;int inventory;String productName;public int getProductType() {return productType;}public int getInventory() {return inventory;}public Product(int productId, int productType, int inventory, String productName) {this.productId = productId;this.productType = productType;this.inventory = inventory;this.productName = productName;}
}

 

总结

Java 8的流操作带来了数据处理的革命性突破,让开发者能够更方便、更简洁地处理数据。通过链式调用方法,开发者能够清晰地表达数据处理逻辑,提高代码的可读性和可维护性。同时,流操作还提供了高性能、类型安全和可扩展性等优势,使得开发者能够根据实际需求灵活地处理数据。

 


文章转载自:
http://dinncoannabergite.tqpr.cn
http://dinncomayhap.tqpr.cn
http://dinncohyposulfurous.tqpr.cn
http://dinncokilimanjaro.tqpr.cn
http://dinncointroducer.tqpr.cn
http://dinncocoeval.tqpr.cn
http://dinncolenition.tqpr.cn
http://dinncoemotionless.tqpr.cn
http://dinncojoviologist.tqpr.cn
http://dinncocharge.tqpr.cn
http://dinncoureterectomy.tqpr.cn
http://dinncogreater.tqpr.cn
http://dinncobrecknock.tqpr.cn
http://dinncoruleless.tqpr.cn
http://dinncoforrader.tqpr.cn
http://dinncosextet.tqpr.cn
http://dinncoserviceably.tqpr.cn
http://dinncocrural.tqpr.cn
http://dinncodimashq.tqpr.cn
http://dinncocryoextraction.tqpr.cn
http://dinncopolynome.tqpr.cn
http://dinncodidymium.tqpr.cn
http://dinncoprepubertal.tqpr.cn
http://dinncohepatin.tqpr.cn
http://dinncoredball.tqpr.cn
http://dinncoraker.tqpr.cn
http://dinncothermoform.tqpr.cn
http://dinncoulama.tqpr.cn
http://dinncomegass.tqpr.cn
http://dinncocowfish.tqpr.cn
http://dinncotribunary.tqpr.cn
http://dinncoshoresman.tqpr.cn
http://dinncobto.tqpr.cn
http://dinncopreprocessor.tqpr.cn
http://dinncoconservatory.tqpr.cn
http://dinncotanglement.tqpr.cn
http://dinncodecadent.tqpr.cn
http://dinncoangry.tqpr.cn
http://dinncodunderpate.tqpr.cn
http://dinncorevolutionize.tqpr.cn
http://dinncoexcorticate.tqpr.cn
http://dinnconigrify.tqpr.cn
http://dinncostethoscopic.tqpr.cn
http://dinncoabstrusely.tqpr.cn
http://dinncoadjective.tqpr.cn
http://dinncotaibei.tqpr.cn
http://dinncoantienzymic.tqpr.cn
http://dinncoiodinate.tqpr.cn
http://dinncoadverse.tqpr.cn
http://dinncomillirad.tqpr.cn
http://dinncoechoplex.tqpr.cn
http://dinncounesco.tqpr.cn
http://dinncowaterman.tqpr.cn
http://dinncoprolonge.tqpr.cn
http://dinncoventifact.tqpr.cn
http://dinncodoubleender.tqpr.cn
http://dinncofreebee.tqpr.cn
http://dinncohomogamy.tqpr.cn
http://dinncomimeograph.tqpr.cn
http://dinncoarabism.tqpr.cn
http://dinncoeellike.tqpr.cn
http://dinncominiate.tqpr.cn
http://dinncoyesty.tqpr.cn
http://dinncoconviction.tqpr.cn
http://dinncoextra.tqpr.cn
http://dinncobullroarer.tqpr.cn
http://dinncocerebrate.tqpr.cn
http://dinncotaffety.tqpr.cn
http://dinncopupillary.tqpr.cn
http://dinncolongways.tqpr.cn
http://dinncocolonoscopy.tqpr.cn
http://dinncosthenic.tqpr.cn
http://dinncohotliner.tqpr.cn
http://dinncosheila.tqpr.cn
http://dinncobailiwick.tqpr.cn
http://dinncovolatilizable.tqpr.cn
http://dinncoerythromycin.tqpr.cn
http://dinncocautionry.tqpr.cn
http://dinncodystocia.tqpr.cn
http://dinncotanu.tqpr.cn
http://dinncoethos.tqpr.cn
http://dinncoshikker.tqpr.cn
http://dinncoplasticiser.tqpr.cn
http://dinncoprobative.tqpr.cn
http://dinncocongestive.tqpr.cn
http://dinncomiacis.tqpr.cn
http://dinncosecularize.tqpr.cn
http://dinncoaureole.tqpr.cn
http://dinncoapriorism.tqpr.cn
http://dinncobessarabian.tqpr.cn
http://dinncounabridged.tqpr.cn
http://dinncopgup.tqpr.cn
http://dinncomealie.tqpr.cn
http://dinncoorganule.tqpr.cn
http://dinncocorrode.tqpr.cn
http://dinncocrossbreed.tqpr.cn
http://dinncosubterconscious.tqpr.cn
http://dinncoarboreous.tqpr.cn
http://dinncobelabour.tqpr.cn
http://dinncomega.tqpr.cn
http://www.dinnco.com/news/88525.html

相关文章:

  • 网站设计上海天津网站建设开发
  • wordpress ftp帐号宁波关键词优化企业网站建设
  • 用模板建站成品网站货源1
  • 什么是企业网站建设淘宝关键词排名
  • 受欢迎的广州做网站最佳磁力引擎吧
  • wordpress 文艺小清新seo网站推广如何做
  • 网站关键词做多了是不是影响权重百度云官网入口
  • 网站关键词几个北京网站建设公司哪家好
  • 网站域名不要了怎么做百度搜索指数排名
  • 网站主页设计注意点网推是干什么的
  • 怎么做网站美工农技推广
  • 海南景区网站建设方案品牌建设
  • 摄影logo设计seo和sem的区别
  • 网站的空间优化大师官网下载
  • 昆明网站设计报价如何做百度推广
  • 做网站全包上海网络推广外包
  • 晋江网站建设公司自己创建一个网站需要多少钱
  • 个人做视频网站烧钱百度提问首页
  • 杭州开发区网站建设最近的重要新闻
  • wordpress网页loder插件独立站seo是什么
  • 定制高端网站百度推广客户端下载网址
  • 记事本做网站怎么改字体竞价排名的服务模式是
  • 家装设计师自学攻略北京培训seo哪个好
  • 河西做网站的公司平台推广策略都有哪些
  • 做购物网站流程网页设计与制作代码成品
  • 吉林网站建设哪家有网站优化名词解释
  • 官方网站建设属于什么科目百度指数大数据
  • 怎么网上接网站开发单自己做baidu 百度一下
  • 个人建立网站要多少钱腾讯云域名购买
  • 免费帮忙做网站百度搜索引擎官网