# Java8之Stream终止操作 **Repository Path**: fpfgitmy_admin/java8-stream-end-operation ## Basic Information - **Project Name**: Java8之Stream终止操作 - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2021-04-28 - **Last Updated**: 2021-04-28 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ### Stream的终止操作 + 终端操作会从流的流水线生成结果。其结果可以是任何不是流的值,例如:List、Integer,甚至void + 流进行了终止操作后,不能再次使用 #### 匹配与查找 | 方法 | 描述 | 扩展 | | --- | --- | --- | | allMatch(Predicate p) | 检查是否匹配所有元素 | - | | anyMatch(Predicate p) | 检查是否至少匹配一个元素 | - | | noneMatch(Predicate p) | 检查是否没有匹配所有元素 | - | | findFirst() | 返回第一个元素 | - | | findAny() | 返回当前流中的任意元素 | - | | max() | 返回元素中的最大值,一般配合map使用 | - | | count() | 和filter配合使用,返回指定条件的数量| - | | min() | 返回元素中的最小值,一般配合实现Comparator的类使用 | - | | forEach() | 流的遍历操作 | 内部迭代(使用Collection接口需要用户去做迭代称为外部迭代。相反Stream API使用内部迭代) | ##### 代码示例 ``` // 1-匹配与查找 @Test public void test1() { List goods = Goods.getGoods(); // 集合中的手机价格是否都大于4000 boolean allMatch = goods.stream().allMatch(g -> g.getPrice() > 4000.0); System.out.println("返回的结果" + allMatch); // 集合中手机价格至少一个大于4000 boolean anyMatch = goods.stream().anyMatch(g -> g.getPrice() > 4000.0); System.out.println("返回的结果" + anyMatch); // 集合中手机价格没有大于4000的 boolean noneMatch = goods.stream().noneMatch(g -> g.getPrice() > 4000.0); System.out.println("返回的结果" + noneMatch); // 获取第一个元素 Optional first = goods.stream().findFirst(); System.out.println("第一个元素" + first.get().toString()); // 获取随机一个元素 Optional any = goods.stream().findAny(); System.out.println("随机一个元素" + any.get().toString()); Optional max = goods.stream().map(g -> g.getPrice()).max(Double::compare); System.out.println("获取到的手机价格最大的值" + max.get()); long count = goods.stream().filter(g -> g.getPrice() > 4000).count(); System.out.println("手机价格大于4000的个数" + count); Optional min = goods.stream().min((g1, g2) -> g1.getPrice().compareTo(g2.getPrice())); System.out.println("获取到的手机价格最小的手机" + min.get()); // 进行遍历,内部迭代 goods.stream().forEach(System.out::println); } ``` #### 归约 + map和reduce的连接通常称为map-reduce模式 + map做的是映射,reduce做的是sum | 方法 | 描述 | 扩展 | | --- | --- | --- | |reduce(T iden,BinaryOperator b) | 可以将流中元素反复结合起来,得到一个值,返回T,参数一为初始值,参数二为计算方法 |- | | reduce(BinaryOperator accumulator) | 传入计算方法,反之值T | - | ##### 代码示例 ``` @Test public void test2() { // 计算1-10自然数的和 List list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); // 第一个参数为初始值,第二个参数为继承BiFunction函数式接口,使用方法引用 Integer reduce = list.stream().reduce(0, Integer::sum); System.out.println("计算之后的值" + reduce); // 计算价格的总和 List goods = Goods.getGoods(); // 使用方法引用 Optional reduce1 = goods.stream().map(Goods::getPrice).reduce(Double::sum); System.out.println("求和之后的值" + reduce1.get()); // 使用Lambda表达式 Optional reduce2 = goods.stream().map(Goods::getPrice).reduce((p1, p2) -> p1 + p2); System.out.println("求和之后的值" + reduce2.get()); } ``` #### 收集 + Collector接口中的方法实现决定了如何对流执行收集操作(如收集到List、Set、Map) + 另外Collectors实用类提供了很多静态方法,可以很方便的创建常见收集器实例 | 方法 | 描述 | 扩展 | | --- | --- | --- | | collect(Collector c) | 将流转换成其他形式,接受一个Collector接口的实现,用于给Stream中元素做汇总的方法 | - | ##### 代码示例 ``` @Test public void test3() { List goods = Goods.getGoods(); // 获取手机价格大于4000的商品 List newGoods = goods.stream().filter(g -> g.getPrice() > 4000.0).collect(Collectors.toList()); System.out.println("获取到的对应条件的商品" + newGoods.toString()); // 进行商品金额的顺序排序 List collect = goods.stream().sorted((g1, g2) -> Double.compare(g1.getPrice(), g2.getPrice())).collect(Collectors.toList()); System.out.println("排序后的数据" + collect.toString()); } ``` ##### Collectors的方法 | 方法 | 返回类型 | 示意 | 扩展 | | --- | --- | --- | --- | | toList | List | 把流中元素收集到List | - | | toSet | Set | 把流中元素收集到Set | - | | toCollection | Collection | 把流中元素收集到创建的集合 | - | | counting | Long | 计算流中元素的个数 | - | | summingint | Integer | 对流中元素的整数属性求和 | - | | averagingInt | Double | 计算流中元素Integer属性的平均值 | - | | summarizingInt | IntSummaryStatistics | 收集流中Integer属性的统计值,如:平均值 | - |