我是靠谱客的博主 复杂鞋子,这篇文章主要介绍Java8中Collectors的使用averagingDouble,averagingInt,averagingLongcollectingAndThencountinggroupingBygroupingByConcurrentjoiningmappingmaxBy,minBypartitioningByreducingsummarizingDouble,summarizingInt,summarizingLongsummingDouble,summingInt,summingLongtoC,现在分享给大家,希望可以做个参考。

前言: 基本类型的流没有这个用法

文章目录

  • averagingDouble,averagingInt,averagingLong
  • collectingAndThen
  • counting
  • groupingBy
  • groupingByConcurrent
  • joining
  • mapping
  • maxBy,minBy
  • partitioningBy
  • reducing
  • summarizingDouble,summarizingInt,summarizingLong
  • summingDouble,summingInt,summingLong
  • toCollection
  • toConcurrentMap
  • toList,toSet
  • toMap

averagingDouble,averagingInt,averagingLong

平均值计算,返回的都是Double类型

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
List<Dog> users = Arrays.asList( new Dog("a",9), new Dog("b",10), new Dog("c",10), new Dog("d",13), new Dog("e",14)); Double collect = users.stream().collect(Collectors.averagingDouble(x -> x.getAge())); System.out.println(collect);//11.2 Double collect1 = users.stream().collect(Collectors.averagingInt(x -> x.getAge())); System.out.println(collect1);//11.2 Double collect2 = users.stream().collect(Collectors.averagingLong(x -> x.getAge())); System.out.println(collect2);//11.2

collectingAndThen

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
List<Dog> users = Arrays.asList( new Dog("a",9), new Dog("b",10), new Dog("c",10), new Dog("d",13), new Dog("e",14)); Map<Integer, Long> collect = users.stream().collect(Collectors.groupingBy(x -> x.getAge(), Collectors.counting())); System.out.println(collect);//{9=1, 10=2, 13=1, 14=1} Integer collect1 = users.stream().collect(Collectors.collectingAndThen(Collectors.groupingBy(x -> x.getAge()), map -> map.size())); System.out.println(collect1);//4
  • 第一个参数为要做的操作,第二个参数对第一个参数收集到的结果进行如何的处理

counting

复制代码
1
2
3
4
5
6
//创建数组 元素为0~9 List<Integer> collect = Stream.iterate(0, x -> x + 1).limit(10).collect(Collectors.toList()); Long collect1 = collect.stream().collect(Collectors.counting()); System.out.println(collect1);

groupingBy

  1. 一个参数表示按照哪个值来分组,这个值为key,其中的value就为符合k的集合
  2. 两个参数表示:第一个参数同上,第二个参数可以对value集合再次进行流操作
  3. 三个参数表示:第一个参数同上,第二个参数表示要返回什么样的Map,第三个参数可以对value集合再次进行流操作
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
List<Dog> users = Arrays.asList( new Dog("a",9), new Dog("b",10), new Dog("c",10), new Dog("d",13), new Dog("e",14)); //按照年龄来分类,key为年龄,value为相同年龄的集合 Map<Integer, List<Dog>> collect = users.stream().collect(Collectors.groupingBy(x -> x.getAge())); //{9=[Dog{name='a', age=9}], 10=[Dog{name='b', age=10}, Dog{name='c', age=10}], 13=[Dog{name='d', age=13}], 14=[Dog{name='e', age=14}]} System.out.println(collect); //{9=[], 10=[Dog{name='c', age=10}], 13=[], 14=[]} Map<Integer, Set<Dog>> c = users.stream().collect(Collectors.groupingBy(x -> x.getAge(), Collectors.filtering(x -> x.getName().equals("c"), Collectors.toSet()))); // TreeMap<Object, Set<Dog>> collect1 = users.stream().collect(Collectors.groupingBy(x -> x.getAge(), TreeMap::new, Collectors.toSet())); System.out.println(collect1);

groupingByConcurrent

使用同groupingBy,但是返回的是线程安全的集合。

joining

复制代码
1
2
3
4
5
6
7
8
String[] strings = {"gs","sb","dpz"}; String collect = Arrays.stream(strings).collect(Collectors.joining()); String collect1 = Arrays.stream(strings).collect(Collectors.joining(",")); String collect2 = Arrays.stream(strings).collect(Collectors.joining(",", "[", "]")); System.out.println(collect);//gssbdpz System.out.println(collect1);//gs,sb,dpz System.out.println(collect2);//[gs,sb,dpz]

mapping

复制代码
1
2
3
4
5
6
7
8
//创建数组 元素为0~9 List<Integer> collect = Stream.iterate(0, x -> x + 1).limit(10).collect(Collectors.toList()); //过滤 Set<Integer> collect1 = collect.stream().collect(Collectors.mapping(x -> x, Collectors.toSet())); //等价于 Set<Integer> collect2 = collect.stream().map(x -> x).collect(Collectors.toSet());

maxBy,minBy

  1. maxBy:return (a, b) -> comparator.compare(a, b) >= 0 ? a : b 如果返回的小于0,取b
  2. minBy:return (a, b) -> comparator.compare(a, b) <= 0 ? a : b 如果返回结果小于0,取a
复制代码
1
2
3
4
5
6
7
8
//创建数组 元素为0~9 List<Integer> collect = Stream.iterate(0, x -> x + 1).limit(10).collect(Collectors.toList()); Optional<Integer> collect1 = collect.stream().collect(Collectors.maxBy((x, y) -> -1)); Optional<Integer> collect2 = collect.stream().collect(Collectors.minBy((x, y) -> -1)); System.out.println(collect1); //9 System.out.println(collect2); //0

partitioningBy

1.一个参数: 根据过滤把结果集分为两组存在Map中,键为true的为一组,键为false的为一组,值为List存放元素
2. 两个参数:同上,但是第二个参数表示再次过滤Map中的Value。

复制代码
1
2
3
4
5
6
7
8
//创建数组 元素为0~9 List<Integer> collect = Stream.iterate(0, x -> x + 1).limit(10).collect(Collectors.toList()); Map<Boolean, List<Integer>> collect2 = collect.stream().collect(Collectors.partitioningBy(x -> x > 2)); System.out.println(collect2); // {false=[0, 1, 2], true=[3, 4, 5, 6, 7, 8, 9]} Map<Boolean, Integer> collect1 = collect.stream().collect(Collectors.partitioningBy(x -> x > 2, Collectors.reducing(0,(x, y) -> Math.min(x, y)))); System.out.println(collect1); // {false=0, true=0}

reducing

复制代码
1
2
3
4
5
6
7
8
9
10
//创建数组 元素为0~9 List<Integer> collect = Stream.iterate(0, x -> x + 1).limit(10).collect(Collectors.toList()); //因为提供了第一个参数,确定了类型,返回值为该类型对象,第二个参数x,y为流中的两个值进行操作 Integer collect1 = collect.stream().collect(Collectors.reducing(0, (x, y) -> Math.min(x, y))); //返回值Optional<T>,第二个参数x,y为流中的两个值进行操作 Optional<Integer> collect2 = collect.stream().collect(Collectors.reducing((x, y) -> Math.min(x, y))); //因为提供了第一个参数,确定了类型,返回值为该类型对象,第二个参数还可以对每个元素进行一次操作,第三个参数x,y为流中的两个值进行操作 Integer collect3 = collect.stream().collect(Collectors.reducing(0, x -> x+1, (x, y) -> Math.max(x, y)));

summarizingDouble,summarizingInt,summarizingLong

方法描述
summarizingDouble(ToDoubleFunction<? super T> mapper)接口实现方法为传入一个当前元素,返回值为对应的基本类型,收集后得到一个xxxSummaryStatistics实例,属性包括最大,最小等属性
summarizingInt(ToIntFunction<? super T> mapper)同上
summarizingLong(ToLongFunction<? super T> mapper)同上
复制代码
1
2
3
4
5
6
7
8
9
10
//创建数组 元素为0~9 List<Integer> collect = Stream.iterate(0, x -> x + 1).limit(10).collect(Collectors.toList()); //Collectors.summarizingInt(ToIntFunction<? super T> mapper) IntSummaryStatistics collect1 = collect.stream().collect(Collectors.summarizingInt(x -> x)); System.out.println(collect1);//IntSummaryStatistics{count=10, sum=45, min=0, average=4.500000, max=9} DoubleSummaryStatistics collect2 = collect.stream().collect(Collectors.summarizingDouble(x -> x)); System.out.println(collect2);//DoubleSummaryStatistics{count=10, sum=45.000000, min=0.000000, average=4.500000, max=9.000000} LongSummaryStatistics collect3 = collect.stream().collect(Collectors.summarizingLong(x -> x)); System.out.println(collect3);//LongSummaryStatistics{count=10, sum=45, min=0, average=4.500000, max=9}

summingDouble,summingInt,summingLong

方法描述
summingDouble(ToDoubleFunction<? super T> mapper)接口实现方法为传入一个当前元素,返回值为对应的基本类型,收集后得到一个对应的包装类,值就为和
summingInt(ToIntFunction<? super T> mapper)同上
summingLong(ToLongFunction<? super T> mapper)同上
复制代码
1
2
3
4
5
6
7
//创建数组 元素为0~9 List<Integer> collect = Stream.iterate(0, x -> x + 1).limit(10).collect(Collectors.toList()); //Collectors.summarizingInt(ToIntFunction<? super T> mapper) Double collect1 = collect.stream().collect(Collectors.summingDouble(x -> x)); Integer collect2 = collect.stream().collect(Collectors.summingInt(x -> x)); Long collect3 = collect.stream().collect(Collectors.summingLong(x -> x));

toCollection

复制代码
1
2
3
4
5
Integer[] a = {1,2,3,4,5,6,7,8,9,10}; //Collectors.toCollection 返回指定集合类型,要求:必须为collectors的子类 Arrays.stream(a).collect(Collectors.toCollection(TreeSet::new)); Arrays.stream(a).collect(Collectors.toCollection(()->new TreeSet<>()));

toConcurrentMap

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
Integer[] a = {1,2,3,4,5,6,7,8,9,10}; //toConcurrentMap.toMap 使用 //1.两个参数,一个key,一个value 组成任意Map(key,value的类型由返回值类型决定) //{1=1, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8, 9=9, 10=10} ConcurrentMap<String, String> collect = Arrays.stream(a).collect(Collectors.toConcurrentMap(x -> x.toString(), x -> x.toString())); //2.三个参数,第三个参数决定key冲突时怎么处理,(x,y) 代表两个冲突k的value值 //{1=11} key全为1,value每次都保存最新的那个 ConcurrentMap<String, String> collect1 = Arrays.stream(a).collect(Collectors.toConcurrentMap((x) -> "1", x -> String.valueOf(x+1),(x, y)->y)); 3.四个参数,第四个代表要返回的Map类型,默认ConcurrentMap,只允许ConcurrentMap的子类 ConcurrentSkipListMap<String, String> collect2 = Arrays.stream(a).collect(Collectors.toConcurrentMap((x) -> "1", x -> String.valueOf(x + 1), (x, y) -> y, ConcurrentSkipListMap::new));

toList,toSet

复制代码
1
2
3
4
5
6
7
Integer[] a = {1,2,3,4,5,6,7,8,9,10,1}; //去重,组成set [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Set<Integer> set = Arrays.stream(a).collect(Collectors.toSet()); //直接组成list [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1] List<Integer> list = Arrays.stream(a).collect(Collectors.toList());

toMap

使用例子:

复制代码
1
2
3
4
5
6
7
8
9
10
11
Integer[] a = {1,2,3,4,5,6,7,8,9,10}; //Collectors.toMap 使用 //1.两个参数,一个key,一个value 组成任意Map(key,value的类型由返回值类型决定) //{1=1, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8, 9=9, 10=10} Map<String, String> collect = Arrays.stream(a).collect(Collectors.toMap(x -> x.toString(), x -> x.toString())); //2.三个参数,第三个参数决定key冲突时怎么处理,(x,y) 代表两个冲突k的value值 //{1=11} key全为1,value每次都保存最新的那个 Map<String, String> collect1 = Arrays.stream(a).collect(Collectors.toMap((x) -> "1", x -> String.valueOf(x+1),(x,y)->y)); //3.四个参数,第四个代表要返回的Map类型,默认hashMap Map<String, String> collect2 = Arrays.stream(a).collect(Collectors.toMap((x) -> "1", x -> String.valueOf(x+1),(x,y)->y,TreeMap::new));

最后

以上就是复杂鞋子最近收集整理的关于Java8中Collectors的使用averagingDouble,averagingInt,averagingLongcollectingAndThencountinggroupingBygroupingByConcurrentjoiningmappingmaxBy,minBypartitioningByreducingsummarizingDouble,summarizingInt,summarizingLongsummingDouble,summingInt,summingLongtoC的全部内容,更多相关Java8中Collectors内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(78)

评论列表共有 0 条评论

立即
投稿
返回
顶部