文章目录
- toList、toSet
- toMap
- toConcurrentMap
- toCollection
- joining
- collectingAndThen
- groupingBy
- groupingByConcurrent
- partitioningBy
- counting
- Stream.count() 实现相同功能
- maxBy
- Stream.max() 实现相同功能
- minBy
- Stream.min() 实现相同功能
- summingXXX
- Stream.sum() 实现相同功能
- summarizingXXX
- mapping
- Stream.map 实现相同功能
- reducing
- Stream.reduce 实现相同功能
- averagingDouble
- Stream.average() 实现相同功能
Collectors
是 Java 8 加入的操作类,位于
java.util.stream
包下。它会根据不同的策略将元素收集归纳起来,比如最简单常用的是将元素装入
Map、Set、List
等可变容器中
toList、toSet
Collector<T, ?, List<T>> toList()
返回一个将输入元素累积到新List中的Collector
Collector<T, ?, Set<T>> toSet()
返回一个将输入元素累积到新Set中的Collector
Stream<String> language = Stream.of("java", "php","java");
List<String> list = language.collect(Collectors.toList());
System.out.println(list);//[java, php, java]
Stream<String> language1 = Stream.of("java", "php","java");
Set<String> set = language1.collect(Collectors.toSet());
System.out.println(set);//[java, php]
toMap
Collector<T, ?, Map<K,U>> toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper)
返回一个将输入元素累积到新map中的Collector,keyMapper生成key的函数,valueMapper生成value的函数,当可以相同时报错java.lang.IllegalStateException: Duplicate key
Collector<T, ?, Map<K,U>> toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper, BinaryOperator<U> mergeFunction)
返回一个将输入元素累积到新map中的Collector,keyMapper生成key的函数,valueMapper生成value的函数,mergeFunction处理相同key的元素
Collector<T, ?, M> toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper, BinaryOperator<U> mergeFunction, Supplier<M> mapSupplier)
返回一个将输入元素累积到新map中的Collector,keyMapper生成key的函数,valueMapper生成value的函数,mergeFunction处理相同key的元素,mapSupplier一个函数,它返回一个新的指定实现类的空Map ,结果将插入其中
Stream<String> language2 = Stream.of("java", "php");
Map<String, Integer> map = language2.collect(Collectors.toMap(o -> o, o -> o.length()));
System.out.println(map);//{java=4, php=3}
Stream<String> language21 = Stream.of("java", "php","java");
Map<String, Integer> map21 = language21.collect(Collectors.toMap(o -> o, o -> o.length(), (o1, o2) -> o1));
System.out.println(map21);//{java=4, php=3}
Stream<String> language22 = Stream.of("java", "php","java");
LinkedHashMap<String, Integer> map22 = language22.collect(Collectors.toMap(o -> o, o -> o.length(), (o1, o2) -> o1, LinkedHashMap::new));
System.out.println(map22);//{java=4, php=3}
toConcurrentMap
和toMap相似,但是使用并发ConcurrentMap承装
Collector<T, ?, ConcurrentMap<K,U>> toConcurrentMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper)
返回一个将输入元素累积到新并发map中的Collector,keyMapper生成key的函数,valueMapper生成value的函数,当可以相同时报错java.lang.IllegalStateException: Duplicate key
Collector<T, ?, ConcurrentMap<K,U>> toConcurrentMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper, BinaryOperator<U> mergeFunction)
返回一个将输入元素累积到新map中的Collector,keyMapper生成key的函数,valueMapper生成value的函数,mergeFunction处理相同key的元素
Collector<T, ?, M> toConcurrentMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper, BinaryOperator<U> mergeFunction, Supplier<M> mapSupplier)
返回一个将输入元素累积到新map中的Collector,keyMapper生成key的函数,valueMapper生成value的函数,mergeFunction处理相同key的元素,mapSupplier一个函数,它返回一个新的指定实现类的空Map ,结果将插入其中
Stream<String> language4 = Stream.of("java", "php");
ConcurrentMap<String, Integer> concurrentMap = language4.collect(Collectors.toConcurrentMap(o -> o, o -> o.length()));
System.out.println(concurrentMap);//{java=4, php=3}
Stream<String> language41 = Stream.of("java", "php","java");
ConcurrentMap<String, Integer> concurrentMap1 = language41.collect(Collectors.toConcurrentMap(o -> o, o -> o.length(), (o1, o2) -> o1));
System.out.println(concurrentMap1);//{java=4, php=3}
Stream<String> language42 = Stream.of("java", "php","java");
ConcurrentMap<String, Integer> concurrentMap2 = language42.collect(Collectors.toConcurrentMap(o -> o, o -> o.length(), (o1, o2) -> o1, ConcurrentHashMap::new));
System.out.println(concurrentMap2);//{java=4, php=3}
toCollection
指定承装集合类型
Collector<T, ?, C> toCollection(Supplier<C> collectionFactory)
返回一个将输入元素累积到一个新的指定实现类的Collection中
Stream<String> language3 = Stream.of("java", "php","java");
ArrayList<String> arrayList = language3.collect(Collectors.toCollection(ArrayList::new));
System.out.println(arrayList);//[java, php, java]
joining
拼接元素
Collector<CharSequence, ?, String> joining()
返回一个Collector ,它按照遇到的顺序将输入元素连接成一个String
Collector<CharSequence, ?, String> joining(CharSequence delimiter)
返回一个以遇到顺序连接输入元素的Collector ,由指定的分隔符分隔
Collector<CharSequence, ?, String> joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix)
返回一个Collector ,它按遇到顺序连接输入元素,由指定的分隔符分隔,具有指定的前缀和后缀
Stream<String> language = Stream.of("java", "php","java");
String joining = language.collect(Collectors.joining());
System.out.println(joining);//javaphpjava
Stream<String> language1 = Stream.of("java", "php","java");
String joining1 = language1.collect(Collectors.joining(","));
System.out.println(joining1);//java,php,java
Stream<String> language2 = Stream.of("java", "php","java");
String joining2 = language2.collect(Collectors.joining(",", "[", "]"));
System.out.println(joining2);//[java,php,java]
collectingAndThen
对Collector执行多次加工转换
Collector<T,A,RR> collectingAndThen(Collector<T,A,R> downstream, Function<R,RR> finisher)
对Collector执行多次加工转换
Stream<String> language = Stream.of("java", "php","java");
String collectingAndThen = language.collect(Collectors.collectingAndThen(Collectors.joining(","), String::toUpperCase));
System.out.println(collectingAndThen);//JAVA,PHP,JAVA
groupingBy
对元素分组
Collector<T, ?, Map<K, List<T>>> groupingBy(Function<? super T, ? extends K> classifier)
对类型T输入元素执行“分组”操作,符合条件的元素将组成一个 List 映射到以T为key 的 Map 中
Collector<T, ?, Map<K, D>> groupingBy(Function<? super T, ? extends K> classifier, Collector<? super T, A, D> downstream)
对类型T输入元素执行“分组”操作,符合条件的元素将组成一个 集合 映射到以T为key 的 Map 中,downstream指定返回集合类型
Collector<T, ?, M> groupingBy(Function<? super T, ? extends K> classifier, Supplier<M> mapFactory, Collector<? super T, A, D> downstream)
对类型T输入元素执行“分组”操作,符合条件的元素将组成一个 集合 映射到以T为key 的 mapFactory新生成的map 中,downstream指定返回集合类型
Stream<String> language = Stream.of("java", "php","java");
Map<Integer, List<String>> map = language.collect(Collectors.groupingBy(String::length));
System.out.println(map);//{3=[php], 4=[java, java]}
Stream<String> language2 = Stream.of("java", "php","java");
Map<Integer, Set<String>> map2 = language2.collect(Collectors.groupingBy(String::length, Collectors.toSet()));
System.out.println(map2);//{3=[php], 4=[java]}
Stream<String> language3 = Stream.of("java", "php","java");
LinkedHashMap<Integer, Map<String, Integer>> map3 = language3.collect(Collectors.groupingBy(String::length, LinkedHashMap::new, Collectors.toMap(o -> o, o -> o.length(), (o1, o2) -> o1)));
System.out.println(map3);//{4={java=4}, 3={php=3}}
groupingByConcurrent
分组后返回的是一个并发集合
Collector<T, ?, ConcurrentMap<K, List<T>>> groupingByConcurrent(Function<? super T, ? extends K> classifier)
对类型T输入元素执行“分组”操作,符合条件的元素将组成一个 List 映射到以T为key 的 并发Map 中
Collector<T, ?, ConcurrentMap<K, D>> groupingByConcurrent(Function<? super T, ? extends K> classifier, Collector<? super T, A, D> downstream)
对类型T输入元素执行“分组”操作,符合条件的元素将组成一个 集合 映射到以T为key 的 并发Map 中,downstream指定返回集合类型
Collector<T, ?, M> groupingByConcurrent(Function<? super T, ? extends K> classifier, Supplier<M> mapFactory, Collector<? super T, A, D> downstream)
对类型T输入元素执行“分组”操作,符合条件的元素将组成一个 集合 映射到以T为key 的 mapFactory新生成的并发map 中,downstream指定返回集合类型
Stream<String> language = Stream.of("java", "php","java");
Map<Integer, List<String>> map = language.collect(Collectors.groupingByConcurrent(String::length));
System.out.println(map);//{3=[php], 4=[java, java]}
Stream<String> language2 = Stream.of("java", "php","java");
Map<Integer, Set<String>> map2 = language2.collect(Collectors.groupingByConcurrent(String::length, Collectors.toSet()));
System.out.println(map2);//{3=[php], 4=[java]}
Stream<String> language3 = Stream.of("java", "php","java");
ConcurrentHashMap<Integer, Map<String, Integer>> map3 = language3.collect(Collectors.groupingByConcurrent(String::length, ConcurrentHashMap::new, Collectors.toMap(o -> o, o -> o.length(), (o1, o2) -> o1)));
System.out.println(map3);//{3={php=3}, 4={java=4}}
partitioningBy
Collector<T, ?, Map<Boolean, List<T>>> partitioningBy(Predicate<? super T> predicate)
根据Predicate对输入元素进行判断分区,并将它们组织成Map<Boolean, List<T>>
Collector<T, ?, Map<Boolean, D>> partitioningBy(Predicate<? super T> predicate, Collector<? super T, A, D> downstream)
根据Predicate对输入元素进行判断分区,并将它们组织成Map<Boolean, ?<T>>,downstream指定返回集合类型
Stream<String> language = Stream.of("java", "php","java");
Map<Boolean, List<String>> map = language.collect(Collectors.partitioningBy(o -> o.length() > 3));
System.out.println(map);//{false=[php], true=[java, java]}
Stream<String> language2 = Stream.of("java", "php","java");
Map<Boolean, Set<String>> map2 = language2.collect(Collectors.partitioningBy(o -> o.length() > 3, Collectors.toSet()));
System.out.println(map2);//{false=[php], true=[java]}
counting
Collector<T, ?, Long> counting()
计算输入元素的数量。 如果不存在元素,则结果为 0。
Stream<String> language = Stream.of("java", "php","java");
Long count = language.collect(Collectors.counting());
System.out.println(count);//3
Stream.count() 实现相同功能
Stream<String> language = Stream.of("java", "php","java");
Long count = language.count();
System.out.println(count);//3
maxBy
Collector<T, ?, Optional<T>> maxBy(Comparator<? super T> comparator)
根据comparato比较后返回最大元素的Optional<T>
Stream<String> language = Stream.of("java", "php","java");
Optional<String> maxObj = language.collect(Collectors.maxBy(Comparator.comparingInt(String::length)));
System.out.println(maxObj.get());//java
Stream.max() 实现相同功能
Stream<String> language = Stream.of("java", "php","java");
Optional<String> max = language.max(Comparator.comparingInt(String::length));
System.out.println(max.get());//java
minBy
Collector<T, ?, Optional<T>> minBy(Comparator<? super T> comparator)
根据comparato比较后返回最小元素的Optional<T>
Stream<String> language = Stream.of("java", "php","java");
Optional<String> maxObj = language.collect(Collectors.minBy(Comparator.comparingInt(String::length)));
System.out.println(maxObj.get());//php
Stream.min() 实现相同功能
Stream<String> language = Stream.of("java", "php","java");
Optional<String> max = language.min(Comparator.comparingInt(String::length));
System.out.println(max.get());//php
summingXXX
Collector<T, ?, Double> summingDouble(ToDoubleFunction<? super T> mapper)
求输入元素的Double类型数值函数的总和
Collector<T, ?, Integer> summingInt(ToIntFunction<? super T> mapper)
求输入元素的Integer类型数值函数的总和
Collector<T, ?, Long> summingLong(ToLongFunction<? super T> mapper)
求输入元素的Long类型数值函数的总和
Stream<String> language = Stream.of("java", "php","java");
Double sumDouble = language.collect(Collectors.summingDouble(String::length));
System.out.println(sumDouble);//11.0
Stream<String> language2 = Stream.of("java", "php","java");
Integer sumInt = language2.collect(Collectors.summingInt(String::length));
System.out.println(sumInt);//11
Stream<String> language3 = Stream.of("java", "php","java");
Long sumLong = language3.collect(Collectors.summingLong(String::length));
System.out.println(sumLong);//11
Stream.sum() 实现相同功能
当Stream的类似是数字时,可以用Stream.sum()实现相同功能
DoubleStream language = DoubleStream.of(4, 3, 4);
double sumDouble = language.sum();
System.out.println(sumDouble);//11.0
IntStream language2 = IntStream.of(4, 3, 4);
Integer sumInt = language2.sum();
System.out.println(sumInt);//11
LongStream language3 = LongStream.of(4, 3, 4);
Long sumLong = language3.sum();
System.out.println(sumLong);//11
summarizingXXX
汇总统计
Collector<T, ?, DoubleSummaryStatistics> summarizingDouble(ToDoubleFunction<? super T> mapper)
以Double类型数值函数汇总统计,包含了 总数,总和,最小值,最大值,平均值 五个指标。
Collector<T, ?, IntSummaryStatistics> summarizingInt(ToIntFunction<? super T> mapper)
以Integer类型数值函数汇总统计,包含了 总数,总和,最小值,最大值,平均值 五个指标。
Collector<T, ?, LongSummaryStatistics> summarizingLong(ToLongFunction<? super T> mapper)
以Long类型数值函数汇总统计,包含了 总数,总和,最小值,最大值,平均值 五个指标。
Stream<String> language = Stream.of("java", "php","java");
DoubleSummaryStatistics doubleSummaryStatistics = language.collect(Collectors.summarizingDouble(String::length));
System.out.println(doubleSummaryStatistics);//DoubleSummaryStatistics{count=3, sum=11.000000, min=3.000000, average=3.666667, max=4.000000}
Stream<String> language2 = Stream.of("java", "php","java");
IntSummaryStatistics intSummaryStatistics = language2.collect(Collectors.summarizingInt(String::length));
System.out.println(intSummaryStatistics);//IntSummaryStatistics{count=3, sum=11, min=3, average=3.666667, max=4}
Stream<String> language3 = Stream.of("java", "php","java");
LongSummaryStatistics longSummaryStatistics = language3.collect(Collectors.summarizingLong(String::length));
System.out.println(longSummaryStatistics);//LongSummaryStatistics{count=3, sum=11, min=3, average=3.666667, max=4}
mapping
Collector<T, ?, R> mapping(Function<? super T, ? extends U> mapper, Collector<? super U, A, R> downstream)
先对元素使用 mapper 进行再加工操作,然后用另一个downstream 归纳
Stream<String> language = Stream.of("java", "php","java");
List<String> list = language.collect(Collectors.mapping(s -> s.substring(1), Collectors.toList()));
System.out.println(list);//[ava, hp, ava]
Stream.map 实现相同功能
Stream<String> language2 = Stream.of("java", "php","java");
List<String> list2 = language2.map(s -> s.substring(1)).collect(Collectors.toList());
System.out.println(list2);//[ava, hp, ava]
reducing
Collector<T, ?, Optional<T>> reducing(BinaryOperator<T> op)
根据op比较器来比较大小并分别返回最大值或者最小值,可能拿不到结果
Collector<T, ?, T> reducing(T identity, BinaryOperator<T> op)
根据op比较器来比较大小并分别返回最大值或者最小值,identity为基准值,基准值也参与比较,没有结果时返回基准值,否则返回比较结果
Collector<T, ?, U> reducing(U identity, Function<? super T, ? extends U> mapper, BinaryOperator<U> op)
先对元素进行mapper映射处理,再根据op比较器来比较大小并分别返回最大值或者最小值,identity为基准值,基准值也参与比较,没有结果时返回基准值,否则返回比较结果
Stream<String> language = Stream.of("java", "php","java");
Comparator<String> comparing = Comparator.comparing(String::length);
Optional<String> obj = language.collect(Collectors.reducing(BinaryOperator.minBy(comparing)));
System.out.println(obj);//Optional[php]
Stream<String> language2 = Stream.of("java", "php","java");
Comparator<String> comparing2 = Comparator.comparing(String::length);
String objStr = language2.collect(Collectors.reducing("androd", BinaryOperator.maxBy(comparing2)));
System.out.println(objStr);//androd
Stream<String> language3 = Stream.of("java", "php","java");
Function<String, String> mapper = str -> {return str.substring(1);};
Comparator<String> comparing3 = Comparator.comparing(String::length);
String objStr2 = language3.collect(Collectors.reducing("androd", mapper, BinaryOperator.maxBy(comparing3)));
System.out.println(objStr2);//androd
Stream.reduce 实现相同功能
Stream<String> language = Stream.of("java", "php","java");
Comparator<String> comparing = Comparator.comparing(String::length);
Optional<String> obj = language.reduce(BinaryOperator.minBy(comparing));
System.out.println(obj);//Optional[php]
Stream<String> language2 = Stream.of("java", "php","java");
Comparator<String> comparing2 = Comparator.comparing(String::length);
String objStr = language2.reduce("androd", BinaryOperator.maxBy(comparing2));
System.out.println(objStr);//androd
Stream<String> language3 = Stream.of("java", "php","java");
BiFunction<String, String, String> mapper = (str1, str2) -> { return str1.length()>str2.length()? str1:str2; };
Comparator<String> comparing3 = Comparator.comparing(String::length);
//第三个参数BinaryOperator.minBy(comparing3)无效,作用是合并各个流结果,故在parallelStream时才生效
String objStr2 = language3.reduce("androd", mapper, BinaryOperator.minBy(comparing3));
System.out.println(objStr2);//androd
averagingDouble
Collector<T, ?, Double> averagingDouble(ToDoubleFunction<? super T> mapper)
对元素进行Double类型数值函数映射处理后,求拼接值
Collector<T, ?, Double> averagingInt(ToIntFunction<? super T> mapper)
对元素进行Int类型数值函数映射处理后,求拼接值
Collector<T, ?, Double> averagingLong(ToLongFunction<? super T> mapper)
对元素进行Long类型数值函数映射处理后,求拼接值
Stream<String> language = Stream.of("java", "php","java");
Double averagingDouble = language.collect(Collectors.averagingDouble(String::length));
System.out.println(averagingDouble);//3.6666666666666665
Stream<String> language2 = Stream.of("java", "php","java");
Double averagingInt2 = language2.collect(Collectors.averagingInt(String::length));
System.out.println(averagingInt2);//3.6666666666666665
Stream<String> language3 = Stream.of("java", "php","java");
Double averagingLong3 = language3.collect(Collectors.averagingLong(String::length));
System.out.println(averagingLong3);//3.6666666666666665
Stream.average() 实现相同功能
当Stream的类似是数字时,可以用Stream.sum()实现相同功能
DoubleStream language = DoubleStream.of(4, 3, 4);
OptionalDouble sumDouble = language.average();
System.out.println(sumDouble.getAsDouble());//3.6666666666666665
IntStream language2 = IntStream.of(4, 3, 4);
OptionalDouble sumInt = language2.average();
System.out.println(sumInt.getAsDouble());//3.6666666666666665
LongStream language3 = LongStream.of(4, 3, 4);
OptionalDouble sumLong = language3.average();
System.out.println(sumLong.getAsDouble());//3.6666666666666665