文章插图
6.4接合(joining) joining
可以将stream中的元素用特定的连接符(没有的话,则直接连接)连接成一个字符串 。
public static void main(String[] args) {List<Person> personList = new ArrayList<>();personList.add(new Person("张三", 1000, 20, "男", "北京"));personList.add(new Person("李四", 2000, 21, "男", "南京"));personList.add(new Person("王五", 3000, 20, "女", "合肥"));personList.add(new Person("赵六", 4000, 22, "男", "合肥"));personList.add(new Person("孙七", 5000, 25, "女", "上海"));String persons = personList.stream().map(p -> p.getName() + "-" + p.getSex() + "-" + p.getSalary()).collect(Collectors.joining(","));System.out.println("所有员工信息:" + persons);}
结果:

文章插图
6.5归约(reducing)
Collectors
类提供的reducing
方法 , 相比于stream
本身的reduce
方法,增加了对自定义归约的支持 。public static void main(String[] args) {List<Person> personList = new ArrayList<>();personList.add(new Person("张三", 6000, 20, "男", "北京"));personList.add(new Person("李四", 6500, 21, "男", "南京"));personList.add(new Person("王五", 7300, 20, "女", "合肥"));personList.add(new Person("赵六", 8000, 22, "男", "合肥"));personList.add(new Person("孙七", 9860, 25, "女", "上海"));// 每个员工减去起征点后的薪资之和(这里个税的算法并不正确,但没想到更好的例子)Integer sum = personList.stream().map(Person::getSalary).reduce(0, (i, j) -> (i + j - 5000));System.out.println("员工扣税薪资总和:" + sum);// stream的reduceOptional<Integer> sum2 = personList.stream().map(Person::getSalary).reduce(Integer::sum);System.out.println("员工薪资总和:" + sum2.get());}
【含具体案例 Java8新特性之Stream流】结果:
文章插图
7.排序(sorted)sorted,中间操作 。有两种排序:
- sorted():自然排序,流中元素需实现Comparable接口
- sorted(Comparator com):Comparator排序器自定义排序
public static void main(String[] args) {List<Person> personList = new ArrayList<>();personList.add(new Person("张三", 16000, 20, "男", "北京"));personList.add(new Person("李四", 8500, 21, "男", "南京"));personList.add(new Person("王五", 7300, 20, "女", "合肥"));personList.add(new Person("赵六", 8000, 22, "男", "合肥"));personList.add(new Person("孙七", 15860, 25, "女", "上海"));// 按工资升序排序(自然排序)List<String> newList = personList.stream().sorted(Comparator.comparing(Person::getSalary)).map(Person::getName).collect(Collectors.toList());// 按工资倒序排序List<String> newList2 = personList.stream().sorted(Comparator.comparing(Person::getSalary).reversed()).map(Person::getName).collect(Collectors.toList());// 先按工资再按年龄升序排序List<String> newList3 = personList.stream().sorted(Comparator.comparing(Person::getSalary).thenComparing(Person::getAge)).map(Person::getName).collect(Collectors.toList());// 先按工资再按年龄自定义排序(降序)List<String> newList4 = personList.stream().sorted((p1, p2) -> {if (p1.getSalary().equals(p2.getSalary())) {return p2.getAge() - p1.getAge();} else {return p2.getSalary() - p1.getSalary();}}).map(Person::getName).collect(Collectors.toList());System.out.println("按工资升序排序:" + newList);System.out.println("按工资降序排序:" + newList2);System.out.println("先按工资再按年龄升序排序:" + newList3);System.out.println("先按工资再按年龄自定义降序排序:" + newList4);}
结果:
文章插图
8.提取/组合流也可以进行合并、去重、限制、跳过等操作 。
public static void main(String[] args) {String[] arr1 = {"a", "b", "c", "d"};String[] arr2 = {"d", "e", "f", "g"};Stream<String> stream1 = Stream.of(arr1);Stream<String> stream2 = Stream.of(arr2);// concat:合并两个流 distinct:去重List<String> newList = Stream.concat(stream1, stream2).distinct().collect(Collectors.toList());// limit:限制从流中获得前n个数据List<Integer> collect = Stream.iterate(1, x -> x + 2).limit(10).collect(Collectors.toList());// skip:跳过前n个数据List<Integer> collect2 = Stream.iterate(1, x -> x + 2).skip(1).limit(5).collect(Collectors.toList());System.out.println("流合并:" + newList);System.out.println("limit:" + collect);System.out.println("skip:" + collect2);}
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 达梦dba_segments指定表名查询到的大小都包含哪些数据
- Object Detection 【YOLOv5】LabVIEW+YOLOv5快速实现实时物体识别含源码
- Object Detection 手把手教你使用LabVIEW OpenCV dnn实现物体识别含源码
- 含蓄的表白的句子有哪些?14个适合表白句子
- 案例分享-https证书链不完整导致请求失败
- 含源码 手把手教你使用LabVIEW OpenCV dnn实现图像分类
- 一 CPS攻击案例——基于脉冲宽度调制PWM的无人机攻击
- 俄罗斯套娃含义-俄罗斯套娃指的是什么?
- NFC 怎么使用(nfc具体使用方法)
- 从缓存入门到并发编程三要素详解 Java中 volatile 、final 等关键字解析案例