IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

十九、Java 8 流 Stream ( 下 )

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

文章永久连接:https://tech.souyunku.com/?p=5022

上一章节 Java 8 流 Stream ( 上 ) 因为时间关系,我们介绍到一半半就停止了。本章节我们继续。

limit() 方法

limit() 方法用于减少( 限制 ) 流中的元素数量。

例如下面的代码段演示了如何使用 limit() 方法只输出 10 个随机数

Random random = new Random();
random.ints().limit(10).forEach(System.out::println);

sorted() 方法

sorted() 方法用于给流中的元素进行排序。

下面的范例演示了如何按照排序顺序打印 10 个随机数

Random random = new Random();
random.ints().limit(10).sorted().forEach(System.out::println);

并发处理

parallelStream() 是需要并发处理的流的替代方案。stream() 方法产生的流只能是串行处理,可以理解为只在一个线程中,按照流中元素的顺序一个接一个的处理。

而并发处理,就是传说中的 map-reduce 方法,可以充分利用多核优势。

需要注意的是,parallelStream() 会打乱流的顺序,也就是返回的序列顺序不一定是输入的序列顺序。

例如下面的代码用于打印序列中的空字符串的数量

List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");

//get count of empty string
int count = strings.parallelStream().filter(string -> string.isEmpty()).count();

因为 stream() 返回是串行流,而 parallelStream() 返回的是并行流。因此在串行和并行之间切换是非常简单的。

收集器 ( Collectors )

收集器 ( Collectors )用于将已经处理的流中的元素组合到一起。

Collectors 类提供了大量方法用于指示如何收集元素。

比如 Collectors.toList() 方法可以将流中的元素收集起来,并转换为列表

List<String>strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
List<String> filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());

System.out.println("Filtered List: " + filtered);

比如 Collectors.joining() 方法可以将流中的元素收集起来,并使用指定的字符串拼接符拼接成一个字符串。

List<String>strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
String mergedString = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.joining(", "));

System.out.println("Merged String: " + mergedString);

统计 ( Statistics )

Java 8 同时新增了大量的统计收集器来来获取流中的元素的一些统计信息。

前提是我们先要在流上调用 summaryStatistics() 方法返回统计信息概要,然后在调用相应的方法来获取具体的统计信息。

例如下面的代码,先调用 summaryStatistics() 方法返回统计概要,然后调用 getMax() 方法获取最大值

List numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);

IntSummaryStatistics stats = integers.stream().mapToInt((x) -> x).summaryStatistics();

System.out.println("Highest number in List : " + stats.getMax());

例如下面的代码,先调用 summaryStatistics() 方法返回统计概要,然后调用 getMin()getSum() 方法获取最小值和所有数字之和

List numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);

IntSummaryStatistics stats = integers.stream().mapToInt((x) -> x).summaryStatistics();

System.out.println("Lowest number in List : " + stats.getMin());
System.out.println("Sum of all numbers : " + stats.getSum());

例如下面的代码,先调用 summaryStatistics() 方法返回统计概要,然后调用 getAverage() 方法获取平均值

List numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);

IntSummaryStatistics stats = integers.stream().mapToInt((x) -> x).summaryStatistics();

System.out.println("Average of all numbers : " + stats.getAverage());

干货推荐

本站推荐:精选优质专栏

附录:Java8 新特性:系列文章


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(70) 打赏



未经允许不得转载:搜云库技术团队 » 十九、Java 8 流 Stream ( 下 )

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367