toList List<T>
把流中所有项目收集到一个 List
Stream<String> stream = Arrays.stream(new String[]{"a", "b", "c"});
List<String> list = stream.collect(Collectors.toList());
toSet Set<T>
把流中所有项目收集到一个 Set,删除重复项
Stream<String> stream = Arrays.stream(new String[]{"a", "b", "c", "a"});
Set<String> set = stream.collect(Collectors.toSet());
counting Long
计算流中元素的个数
Stream<String> stream = Arrays.stream(new String[]{"a", "b", "c", "a"});
long count = stream.collect(Collectors.counting());
summingInt Integer 1
求和
Stream<Integer> stream = Arrays.stream(new Integer[]{1, 3, 5, 7});
int sum = stream.collect(Collectors.summingInt(a -> a));
System.out.println(sum);
averagingInt Double
计算流中项目 Integer 属性的平均值
Stream<Integer> stream = Arrays.stream(new Integer[]{1, 3, 5, 7});
double avg = stream.collect(Collectors.averagingInt(a -> a));
System.out.println(avg);
summarizingInt IntSummaryStatistics
收集关于流中项目 Integer 属性的统计值,例如最大、最小、 总和与平均值
Stream<Integer> stream = Arrays.stream(new Integer[]{1, 3, 5, 7});
IntSummaryStatistics statistics = stream.collect(Collectors.summarizingInt(a -> a));
System.out.println("count:" +statistics.getCount() + ",sum:" + statistics.getSum() + ", min:" + statistics.getMin());
joining String
连接对流中每个项目调用 toString 方法所生成的字符串
Stream<String> stream = Arrays.stream(new String[]{"a", "b", "c"});
String join = stream.collect(Collectors.joining(","));
System.out.println(join);
maxBy Optional<T>
一个包裹了流中按照给定比较器选出的最大元素的 Optional, 或如果流为空则为 Optional.empty()
Stream<Integer> stream = Arrays.stream(new Integer[]{1, 9, 5, 7});
Optional<Integer> max = stream.collect(
Collectors.maxBy(
Comparator.comparing(a -> a)));
System.out.println(max.get());
minBy Optional<T>
一个包裹了流中按照给定比较器选出的最小元素的 Optional,或如果流为空则为 Optional.empty()
Stream<Integer> stream = Arrays.stream(new Integer[]{1, 9, 5, 7});
Optional<Integer> min = stream.collect(
Collectors.minBy(
Comparator.comparing(a -> a)));
System.out.println(min.get());
reducing 归约操作产生的类型
从一个作为累加器的初始值开始,利用 BinaryOperator 与流 中的元素逐个结合,从而将流归约为单个值
Stream<Integer> stream = Arrays.stream(new Integer[]{1, 9, 5, 7});
int sum = stream.collect(
Collectors.reducing(0, a -> a, Integer::sum));
System.out.println(sum);
collectingAndThen 转换函数返回的类型
包裹另一个收集器,对其结果应用转换函数
Stream<Integer> stream = Arrays.stream(new Integer[]{1, 9, 5, 7});
int count = stream.collect(
Collectors.collectingAndThen(
Collectors.toList(), List::size));
System.out.println(count);
groupingBy Map<K, List<T>>
根据项目的一个属性的值对流中的项目作问组,并将属性值作为结果 Map 的键
People p1 = new People("小明", "男", 11);
People p2 = new People("小红", "女", 10);
Stream<People> stream = Arrays.asList(p1, p2).stream();
Map<String, List<People>> map = stream.collect(
Collectors.groupingBy(People::getSex));
public class People {
private String name;
private String sex;
private int age;
public People(String name, String sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
partitioningBy Map<Boolean,List<T>>
根据对流中每个项目应用谓词的结果来对项目进行分区
People p1 = new People("小明", "男", 11);
People p2 = new People("小红", "女", 10);
Stream<People> stream = Arrays.asList(p1, p2).stream();
Map<Boolean, List<People>> map = stream.collect(
Collectors.partitioningBy(p -> "男".equals(p.getSex())));