averagingDouble
返回一个Collector ,它产生应用于输入元素的双值函数的算术平均值。 如果没有元素,结果为0
Stream<String> stream = Stream.of("10","20","30");
double result = stream.collect(Collectors.averagingDouble((x -> Double.parseDouble(x))));
System.out.println(result);
averagingInt
返回一个Collector ,它产生应用于输入元素的整数值函数的算术平均值。 如果没有元素,结果为0。
Stream<String> stream = Stream.of("3","4","5");
double result = stream.collect(Collectors.averagingInt(x-> Integer.parseInt(x)));
System.out.println(result);
collectingAndThen
适应Collector进行额外的整理转换
Stream<String> stream = Stream.of("Hello", "World");
List<String> list = stream.collect(Collectors.collectingAndThen(
Collectors.toList(),
Collections::<String> unmodifiableList));
System.out.println(list);
counting
返回一个Collector类型的接受元素,类型为T ,它计算输入元素的数量。 如果没有元素,结果为0
Stream<String> stream = Stream.of("1","2","3","4");
Long result = stream.collect(Collectors.counting());
System.out.println(result);
groupingBy
返回Collector “由基团”上的类型的输入元件操作实现T ,根据分类功能分组元素,并且在返回的结果Map 。
Stream<String> stream = Stream.of("apple","apple","pear","banana");
Map<String, Long> map =stream.collect(Collectors.groupingBy(
Function.identity(),
Collectors.counting())
);
System.out.println(map);
mapping
适应一个 Collector类型的接受元件 U至类型的一个接受元件 T通过积累前应用映射函数到每个输入元素。
Stream<Employee> stream = Stream.of(
new Employee("joe",30),
new Employee("lin",30));
List<String> names = stream.collect(
Collectors.mapping(x->x.getName(),Collectors.toList()));
System.out.println(names);
按照城市先分组后统计
List<Person> list = Arrays.asList(
new Person("Beijing","zhangsan","zhangsan"),
new Person("Shanghai","lisi", "lisi")
);
Map<String, Set<String>> map = list.stream().collect(
groupingBy(Person::getCity, mapping(Person::getFirstName,toSet())));
System.out.println(map);
partitioningBy
返回一个Collector ,它根据Predicate对输入元素进行Predicate ,并将它们组织成Map<Boolean, List> 。 Map返回的类型,可变性,可串行性或线程安全性没有Map 。
Stream<String> stream = Stream.of("John","John","Anna","James");
Map<Boolean, List<String>> map = stream.collect(
Collectors.partitioningBy(x->x.length()>4));
System.out.println(map);
reducing
返回一个Collector ,它在指定的Collector下执行其输入元素的BinaryOperator 。 结果被描述为Optional .
Stream<Employee> stream = Stream.of(new Employee("joe",30),
new Employee("john",40));
Optional<Employee> optEmployee =
stream.collect(Collectors.reducing(BinaryOperator.maxBy(Comparator.comparing(Employee::getAge))));
if (optEmployee.isPresent()){
System.out.println(optEmployee.get().getAge()+","+optEmployee.get().getName());
}
summarizingDouble
返回一个 Collector , double生产映射函数应用于每个输入元素,并返回结果值的汇总统计信息
List<Double> list = Arrays.asList(23.43,23.32,8.7);
Stream<Double> stream = list.stream();
DoubleSummaryStatistics doubleSummaryStatistics = stream.collect(Collectors.summarizingDouble(x->x));
System.out.println(doubleSummaryStatistics);
summingInt
返回一个Collector ,它产生应用于输入元素的整数值函数的和。 如果没有元素,结果为0。
List<Integer> list = Arrays.asList(10,20,35);
Stream<Integer> integerStream = list.stream();
IntSummaryStatistics intSummaryStatistics = integerStream.collect(Collectors.summarizingInt(x->x));
System.out.println(intSummaryStatistics);
toCollection
返回一个Collector ,按照遇到的顺序将输入元素累加到一个新的Collection中。 Collection由提供的工厂创建。
Stream<String> stream = Stream.of("Hello", "World");
Set<String> set = stream.collect(Collectors.toCollection(TreeSet::new));
System.out.println(set);
toConcurrentMap
Stream<Person> stream = Stream.of(
new Person("beijing","zhangsan","zhangsan"),
new Person("shanghai","lisi","lisi")
);
Map<String,String> map = stream.collect(Collectors.toConcurrentMap(Person::getCity, Person::getFirstName));
System.out.println(map);
toList
返回一个Collector ,它将输入元素List到一个新的List.
Stream<Person> stream = Stream.of(
new Person("beijing","zhangsan","zhangsan"),
new Person("shanghai","lisi","lisi")
);
List<String> list = stream.map(x->x.getFirstName()).collect(Collectors.toList());
toSet
返回一个Collector ,将输入元素Set到一个新的Set
Stream<Person> stream = Stream.of(
new Person("beijing","zhangsan","zhangsan"),
new Person("shanghai","lisi","lisi")
);
Set<String> set = stream.map(x->x.getCity()).collect(Collectors.toSet());
System.out.println(set);
BinaryOperator
上面提到java.util.Collectors里的这些方法大多数里用到了参数BinaryOperator, 这是一个函数式接口,可以用做lambda表达式或者方法引用的赋值对象
@FunctionalInterface
public interface BinaryOpeator<T> extends BiFunction<T,T,T>{
}
Comparator<Integer> comparator = (x, y) -> (x.compareTo(y));
BinaryOperator<Integer> opMax = BinaryOperator.maxBy(comparator);
System.out.println("Max:" + opMax.apply(5, 9));
System.out.println("Max:" + opMax.apply(6, 9));
BinaryOperator<Integer> opMin = BinaryOperator.minBy(comparator);
System.out.println("Min:" + opMin.apply(5, 6));
System.out.println("Min:" + opMin.apply(9, 6));