含具体案例 Java8新特性之Stream流( 四 )


文章插图
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);}结果:

含具体案例 Java8新特性之Stream流

文章插图
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流】结果:
含具体案例 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);}结果:
含具体案例 Java8新特性之Stream流

文章插图
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);}

推荐阅读