java stream中Collectors的用法

简介

在java stream中,我们通常需要将处理后的stream转换成集合类,这个时候就需要用到stream.collect方法。collect方法需要传入一个Collector类型,要实现Collector还是很麻烦的,需要实现好几个接口。

于是java提供了更简单的Collectors工具类来方便我们构建Collector。

下面我们将会具体讲解Collectors的用法。

假如我们有这样两个list:

List<String> list = Arrays.asList("jack", "bob", "alice", "mark");
List<String> duplicateList = Arrays.asList("jack", "jack", "alice", "mark");

上面一个是无重复的list,一个是带重复数据的list。接下来的例子我们会用上面的两个list来讲解Collectors的用法。

Collectors.toList()

List<String> listResult = list.stream().collect(Collectors.toList());
log.info("{}",listResult);

将stream转换为list。这里转换的list是ArrayList,如果想要转换成特定的list,需要使用toCollection方法。

Collectors.toSet()

Set<String> setResult = list.stream().collect(Collectors.toSet());
log.info("{}",setResult);

toSet将Stream转换成为set。这里转换的是HashSet。如果需要特别指定set,那么需要使用toCollection方法。

因为set中是没有重复的元素,如果我们使用duplicateList来转换的话,会发现最终结果中只有一个jack。

Set<String> duplicateSetResult = duplicateList.stream().collect(Collectors.toSet());
log.info("{}",duplicateSetResult);

Collectors.toCollection()

上面的toMap,toSet转换出来的都是特定的类型,如果我们需要自定义,则可以使用toCollection()

List<String> custListResult = list.stream().collect(Collectors.toCollection(LinkedList::new));
log.info("{}",custListResult);

上面的例子,我们转换成了LinkedList。

Collectors.toMap()

toMap接收两个参数,第一个参数是keyMapper,第二个参数是valueMapper:

Map<String, Integer> mapResult = list.stream()
.collect(Collectors.toMap(Function.identity(), String::length));
log.info("{}",mapResult);

如果stream中有重复的值,则转换会报IllegalStateException异常:

Map<String, Integer> duplicateMapResult = duplicateList.stream()
.collect(Collectors.toMap(Function.identity(), String::length));

怎么解决这个问题呢?我们可以这样:

Map<String, Integer> duplicateMapResult2 = duplicateList.stream()
.collect(Collectors.toMap(Function.identity(), String::length, (item, identicalItem) -> item));
log.info("{}",duplicateMapResult2);

在toMap中添加第三个参数mergeFunction,来解决冲突的问题。

Collectors.collectingAndThen()

collectingAndThen允许我们对生成的集合再做一次操作。

List<String> collectAndThenResult = list.stream()
.collect(Collectors.collectingAndThen(Collectors.toList(), l -> {return new ArrayList<>(l);}));
log.info("{}",collectAndThenResult);

Collectors.joining()

Joining用来连接stream中的元素:

String joinResult = list.stream().collect(Collectors.joining());
log.info("{}",joinResult);
String joinResult1 = list.stream().collect(Collectors.joining(" "));
log.info("{}",joinResult1);
String joinResult2 = list.stream().collect(Collectors.joining(" ", "prefix","suffix"));
log.info("{}",joinResult2);

可以不带参数,也可以带一个参数,也可以带三个参数,根据我们的需要进行选择。

Collectors.counting()

counting主要用来统计stream中元素的个数:

Long countResult = list.stream().collect(Collectors.counting());
log.info("{}",countResult);

Collectors.summarizingDouble/Long/Int()

SummarizingDouble/Long/Int为stream中的元素生成了统计信息,返回的结果是一个统计类:

IntSummaryStatistics intResult = list.stream()
.collect(Collectors.summarizingInt(String::length));
log.info("{}",intResult);

输出结果:

22:22:35.238 [main] INFO com.flydean.CollectorUsage - IntSummaryStatistics{count=4, sum=16, min=3, average=4.000000, max=5}

Collectors.averagingDouble/Long/Int()

averagingDouble/Long/Int()对stream中的元素做平均:

Double averageResult = list.stream().collect(Collectors.averagingInt(String::length));
log.info("{}",averageResult);

Collectors.summingDouble/Long/Int()

summingDouble/Long/Int()对stream中的元素做sum操作:

Double summingResult = list.stream().collect(Collectors.summingDouble(String::length));
log.info("{}",summingResult);

Collectors.maxBy()/minBy()

maxBy()/minBy()根据提供的Comparator,返回stream中的最大或者最小值:

Optional<String> maxByResult = list.stream().collect(Collectors.maxBy(Comparator.naturalOrder()));
log.info("{}",maxByResult);

Collectors.groupingBy()

GroupingBy根据某些属性进行分组,并返回一个Map:

Map<Integer, Set<String>> groupByResult = list.stream()
.collect(Collectors.groupingBy(String::length, Collectors.toSet()));
log.info("{}",groupByResult);

Collectors.partitioningBy()

PartitioningBy是一个特别的groupingBy,PartitioningBy返回一个Map,这个Map是以boolean值为key,从而将stream分成两部分,一部分是匹配PartitioningBy条件的,一部分是不满足条件的:

 Map<Boolean, List<String>> partitionResult = list.stream()
.collect(Collectors.partitioningBy(s -> s.length() > 3));
log.info("{}",partitionResult);

看下运行结果:

22:39:37.082 [main] INFO com.flydean.CollectorUsage - {false=[bob], true=[jack, alice, mark]}

结果被分成了两部分。

总结

Collectors是一个非常强大的工具类,希望大家能够灵活使用。

本文的例子https://github.com/ddean2009/learn-java-streams/tree/master/Collectors

欢迎关注我的公众号:程序那些事,更多精彩等着您!

更多内容请访问 www.flydean.com

java stream中Collectors的用法的更多相关文章

  1. JAVA语言中冒号的用法

    近来由于本人要介入android平台的开发,所以就买了本JAVA语言的书学习.学习一段时间来,我的感觉是谭浩强就是厉害,编写的<C编程语言>系列丛书不愧是经典.书中对C语言的介绍既系统又全 ...

  2. Java集合中List的用法

    List接口是Collection接口的子接口,List有一个重要的实现类--ArrayList类,List中的元素是有序排列的而且可重复,所以被称为是序列. List可以精确的控制每个元素的插入位置 ...

  3. java web中cookies的用法 转

    一.什么是cookies? 大家都知道,浏览器与WEB服务器之间是使用HTTP协议进行通信的,当某个用户发出页面请求时,WEB服务器只是简单的进行响应,然后就关闭与该用户的 连接.因此当一个请求发送到 ...

  4. java string中indexOf()常用用法

    Java中字符串中子串的查找共有四种方法,如下: 1.int indexOf(String str) :返回第一次出现的指定子字符串在此字符串中的索引. 2.int indexOf(String st ...

  5. Java类中static的用法

    关于Java中static的使用有以下四种情况: 1.静态成员变量         被static修饰的成员变量,叫静态成员变量或类变量:没有被static修饰的变量,叫实例变量. 两者的区别是:  ...

  6. java——ArrayList中常见方法用法

    package com.xt.list; import java.util.ArrayList; import java.util.Iterator; import java.util.List; p ...

  7. Java线程中yield()的用法

    Thread.yield()方法的作用:暂停当前正在执行的线程,并执行其他线程.(可能没有效果) yield()让当前正在运行的线程回到可运行状态,以允许具有相同优先级的其他线程获得运行的机会.因此, ...

  8. java 10 中 var关键字用法

    引用:https://mp.weixin.qq.com/s/n1tcJ0CywSi0j-YycGPwxg what java10引入了局部变量折断 var用于声明局部变量. 如var user=new ...

  9. Java Stream函数式编程第三篇:管道流结果处理

    一.Java Stream管道数据处理操作 在本号之前写过的文章中,曾经给大家介绍过 Java Stream管道流是用于简化集合类元素处理的java API.在使用的过程中分为三个阶段.在开始本文之前 ...

随机推荐

  1. VirtualBox 安装 Arch Linux 并配置桌面环境

    最近无聊,就找来 Arch Linux 来玩一玩,去 archlinux wiki上看了一下教程.以下是操作过程. 1. 下载镜像,下载地址; 2. 启动 Archlinux 并选择 Boot Arc ...

  2. 使用git上传代码到GitHub

    1.安装git git在Windows上安装很简单,在官网下载git的安装包后打开,然后一路next就好.安装完git之后,在文件夹中右击鼠标,出现Git Bash Here就表示安装完成了. 选择G ...

  3. Hadoop(十):本地IDEA链接远程Hadoop

    本文使用的Hadoop为2.7.7,版本如果不同要下载相应版本的文件 配置本地的Hadoop库(不需完整安装,但是要有环境支持) 下载文件 https://github.com/speedAngel/ ...

  4. 家庭版记账本app进度之编辑框组件

    <EditText>中设置提示信息是用到的语句是android:hint来进行提示语句的书写. android:inputType可以将此编辑框设置为输入密码的编辑框(现实的是小黑点) a ...

  5. javascript 入门 之select2选择本地数据

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"/> <meta lan ...

  6. Linux 磁盘管理篇, 目录管理(二)

    格式化档案系统:                    mke2fs 列出文件系统的整体磁盘使用量            df 评估文件系统的磁盘使用量            du 查看Superbl ...

  7. idle中上传jar包并使用的方法

    创建一个lib目录,将jar包拉到该目录下. 需要导入的Jar包上,点击右键,选择Add as Library…

  8. 34.4 对象流 ObjectOutputStream ObjectInputStream

    * 对象操作流:可以用于读写任意类型的对象 * ObjectOutputStream * writeObject * ObjectOutputStream(OutputStream out) * Ob ...

  9. Struts2-学习笔记系列(11)-使用StrutsTypeConverter

    public class UserConvert extends StrutsTypeConverter { @Override public Object convertFromString(Map ...

  10. Netperf网络性能测试工具详解教程

    本文下载链接: [学习笔记]Netperf网络性能测试工具.pdf 一.Netperf工具简介 1.什么是Netperf ? (1)Netperf是由惠普公司开发的一种网络性能测量工具,主要针对基于T ...