Java8中Collectors的使用
前言: 基本类型的流没有这个用法
文章目录
- 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类型
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.2Double collect1 = users.stream().collect(Collectors.averagingInt(x -> x.getAge()));System.out.println(collect1);//11.2Double collect2 = users.stream().collect(Collectors.averagingLong(x -> x.getAge()));System.out.println(collect2);//11.2
collectingAndThen
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
//创建数组 元素为0~9List<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
- 一个参数表示按照哪个值来分组,这个值为key,其中的value就为符合k的集合
- 两个参数表示:第一个参数同上,第二个参数可以对value集合再次进行流操作
- 三个参数表示:第一个参数同上,第二个参数表示要返回什么样的Map,第三个参数可以对value集合再次进行流操作
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
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);//gssbdpzSystem.out.println(collect1);//gs,sb,dpzSystem.out.println(collect2);//[gs,sb,dpz]
mapping
//创建数组 元素为0~9List<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
- maxBy:return (a, b) -> comparator.compare(a, b) >= 0 ? a : b 如果返回的小于0,取b
- minBy:return (a, b) -> comparator.compare(a, b) <= 0 ? a : b 如果返回结果小于0,取a
//创建数组 元素为0~9List<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); //9System.out.println(collect2); //0
partitioningBy
1.一个参数: 根据过滤把结果集分为两组存在Map中,键为true的为一组,键为false的为一组,值为List存放元素
2. 两个参数:同上,但是第二个参数表示再次过滤Map中的Value。
//创建数组 元素为0~9List<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
//创建数组 元素为0~9List<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) | 同上 |
//创建数组 元素为0~9List<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) | 同上 |
//创建数组 元素为0~9List<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
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
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
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
使用例子:
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类型,默认hashMapMap<String, String> collect2 = Arrays.stream(a).collect(Collectors.toMap((x) -> "1", x -> String.valueOf(x+1),(x,y)->y,TreeMap::new));
Java8中Collectors的使用相关推荐
- java8中 Collectors.groupingBy用法
目录 1.数据准备: 2.分组 按照类目分组: 按照几个属性拼接分组: 根据不同条件分组 3.多级分组 4.按子组收集数据 求总数 求和 把收集器的结果转换为另一种类型 联合其他收集器 Collect ...
- Java8中Collectors详解
文章目录 1.averagingDouble 2.collectingAndThen 3.counting 4.groupingBy 4.1groupingBy(Function) 4.2groupi ...
- java8中的Collectors.groupingBy用法
Collectors.groupingBy根据一个或多个属性对集合中的项目进行分组 数据准备: public Product(Long id, Integer num, BigDecimal pric ...
- Java8中字符串连接(join)收集器 Collectors.joining
** Java8中的字符串连接收集器 ** 在JDK8中,可以采用函数式编程(使用 Collectors.joining 收集器)的方式对字符串进行更优雅的连接. Collectors.joining ...
- java8 中的 Collectors 全解析
1. 前言 相关的文章中提到了 Collectors .相信很多同学对这个比较感兴趣,那我们今天就来研究一下 Collectors . 2. Collectors 的作用 Collectors 是 J ...
- Java8中Lambda表达式的10个例子
Java8中Lambda表达式的10个例子 例1 用Lambda表达式实现Runnable接口 Java代码 //Before Java 8: new Thread(new Runnable() ...
- Java8中Stream流对集合操作
java8中Stream流引入函数式编程思想,主要配合各种接口.lambda表达式.方法引用等方式,为集合的遍历.过滤.映射等提供非常"优雅"的操作方式. Student.java ...
- 字符串拼接还在用StringBuilder?快试试Java8中的StringJoiner吧,真香!
点击关注公众号,Java干货及时送达 前言 之前,我们经常会通过StringBuffer或者StingBuilder对字符串进行拼接,但是你知道Java8中推出的StringJoiner吗?它比前者更 ...
- Java8中的流操作-基本使用性能测试
一.流(Stream)简介 流是 Java8 中 API 的新成员,它允许你以声明式的方式处理数据集合(通过查询语句来表达,而不是临时编写一个实现).这有点儿像是我们操作数据库一样,例如我想要查询出热 ...
最新文章
- Debug的T命令在执行修改寄存器SS 的指令时,下一条指令也紧接着被执行。
- Androidstudio如何正确导入和移出jar包
- python 管道队列_20.2、python进程间通信——队列和管道
- codemirror java代码_codemirror使用(示例代码)
- Winform开发之ADO.NET对象Connection、Command、DataReader、DataAdapter、DataSet和DataTable简介...
- android涂鸦板保存功能,android实现涂鸦,保存涂鸦后的图片,清屏
- php限制只能中文,php如何实现一个账号只能有一个人登录
- Data truncation: Out of range value for column ‘quanity‘ at row 问题解决方案
- java udp发16进制数据_如何通过接口强制发送UDP数据包?
- 数学建模matlab题型,数学建模题型之分类
- excel输出线性拟合曲线的斜率
- 嗖嗖移动大厅JAVA(免费源码分享)
- 我爱淘二次冲刺阶段6
- 双机热备概念原理及实现步骤
- 字节跳动面试锦集(二):项目HR高频面试总结
- 3D数学基础——矩阵、欧拉角和四元数的相互转换与比较
- Js 的 混淆 与反混淆
- matlab 零极点分布图,用matlab设计一个程序,直接输入一组零极点,就可以得到零极点图。这应- 一起装修网...
- android高德:定位蓝点定位到非洲问题
- android bsp入门到精通,网管教程:从入门到精通(软件篇).pdf