Stream是Java 8的新特性,基于lambda表达式,是对集合对象功能的增强,a它专注于对集合对象进行各种高效、方便聚合操作或者大批量的数据操作,提高了编程效率和代码可读性。Collectors通常在Stream处理后,返回转换成集合类时使用,本文主要介绍Java Stream中Collectors.collectingAndThen()、Collectors.joining()和Collectors.counting()的使用,以及相关的示例代码。

1、Collectors.collectingAndThen()的使用

Collectors.collectingAndThen()添加在集合末尾执行的单个操作,先把流中的所有元素传递给第一个参数,然后把生成的集合元素传递给第二个参数来处理。例如,

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Main {
   public static class Person {
     int age;
     String name;
     Person (int age,String name) {
         this.age = age;
         this.name = name;
     }
     public int getAge() {
         return age;
     }
     public void setAge(int age) {
         this.age = age;
     }
     public String getName() {
         return name;
     }
     public void setName(String name) {
         this.name = name;
     }

  }
public static void main(String[] args) {

   List<Person> pList = Arrays.asList(new Person(11,"mike"),new Person(22,"lynn"), new Person(33,"john"), new Person(44,"mickey"), new Person(55,"fiona"));
   String result = pList.stream().collect(Collectors.collectingAndThen(Collectors.maxBy(Comparator.comparing(Person::getAge)), (Optional<Person> p) -> { return p.isPresent() ? p.get().getName() : "none";}
    ));
    System.out.println(result);
    System.exit(0); //success
  }
}

2、Collectors.joining()的使用

Collectors.joining()用来连接stream中的元素,例如,

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Main {
   public static class Person {
     int age;
     String name;
     Person (int age,String name) {
         this.age = age;
         this.name = name;
     }
     public int getAge() {
         return age;
     }
     public void setAge(int age) {
         this.age = age;
     }
     public String getName() {
         return name;
     }
     public void setName(String name) {
         this.name = name;
     }

  }
public static void main(String[] args) {

   List<Person> pList = Arrays.asList(new Person(11,"mike"),new Person(22,"lynn"), new Person(33,"john"), new Person(44,"mickey"), new Person(55,"fiona"));
   String result1 = pList.stream().map(p -> p.getName()).collect(Collectors.joining(" "));
   System.out.println(result1);
   String result2 = pList.stream().map( p -> p.getName())
             .collect(Collectors.joining(" ", "prefix "," suffix"));
   System.out.println(result2);
   String[] strArray = { "C","Java","Py"," : ","https://www.cjavapy.com" };
   String str= Stream.of(strArray)
             .map(arr -> new String(arr))
             .collect(Collectors.joining());
   System.out.println(str);
   System.exit(0); //success
  }
}

3、Collectors.counting()的使用

Collectors.counting()用来统计stream中元素的个数,如下,

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Main {
   public static class Person {
     int age;
     String name;
     Person (int age,String name) {
         this.age = age;
         this.name = name;
     }
     public int getAge() {
         return age;
     }
     public void setAge(int age) {
         this.age = age;
     }
     public String getName() {
         return name;
     }
     public void setName(String name) {
         this.name = name;
     }

  }
public static void main(String[] args) {

    List<Person> pList = Arrays.asList(new Person(11,"mike"),new Person(22,"lynn"), new Person(33,"john"), new Person(44,"mickey"), new Person(55,"fiona"));
    var count = pList.stream().collect(Collectors.counting());
    System.out.println("stream() result :" + count);
    System.exit(0); //success
  }
}

推荐文档