jdk8-collect
toMap
常用方式
public Map<Long, String> getIdNameMap(List<Account> accounts) {
return accounts.stream().collect(Collectors.toMap(Account::getId, Account::getUsername));
}
收集成实体本身map
public Map<Long, Account> getIdAccountMap(List<Account> accounts) {
return accounts.stream().collect(Collectors.toMap(Account::getId, account -> account));
}
//account -> account是一个返回本身的lambda表达式,其实还可以使用Function接口中的一个默认方法代替,使整个方法更简洁优雅:
public Map<Long, Account> getIdAccountMap(List<Account> accounts) {
return accounts.stream().collect(Collectors.toMap(Account::getId, Function.identity()));
}
重复key
public Map<String, Account> getNameAccountMap(List<Account> accounts) {
return accounts.stream().collect(Collectors.toMap(Account::getUsername, Function.identity()));
}
//这个方法可能报错(java.lang.IllegalStateException: Duplicate key),因为name是有可能重复的。toMap有个重载方法,可以传入一个合并的函数来解决key冲突问题:
public Map<String, Account> getNameAccountMap(List<Account> accounts) {
return accounts.stream().collect(Collectors.toMap(Account::getUsername, Function.identity(), (key1, key2) -> key2));
}
//这里只是简单的使用后者覆盖前者来解决key重复问题。
指定具体收集的map
public Map<String, Account> getNameAccountMap(List<Account> accounts) {
return accounts.stream().collect(Collectors.toMap(Account::getUsername, Function.identity(), (key1, key2) -> key2, LinkedHashMap::new));
}
groupingBy
普通分组
accounts.stream()
.collect(Collectors.groupingBy(Account::getUsername));
分组,然后取最大的值
Map<Integer, Optional<Users>> collect = users.stream()
.collect(Collectors.groupingBy(Users::getAge, Collectors.maxBy(Comparator.comparing(Users::getId))));
分组,分组求和
Map<Integer, Optional<Users>> collect = users.stream()
.collect(Collectors.groupingBy(Users::getName, Collectors.summingInt(User::getAge)));
分组后,把原始对象进行转换为新的对象
Map<Integer, List<String>> collect = users.stream()
.collect(Collectors.groupingBy(Users::getAge, Collectors.mapping( item ->{
//当然你这里也可以构建一个新的对象,进行返回
return item.getName();
}, Collectors.toList())));
终止
// 获取所有薪资大于 15000 的员工人数
long count = emps.stream() .filter((x)->x.getSalary() > 15000).count();
// 获取所有薪资大于 15000 的员工人数
long count = emps.stream().map(Employee::getSalary).max(Double::compare);
// 获取所有薪资大于 15000 的员工人数
long count = emps.stream().min((x,y)->Double.compare(x.getSalary(), y.getSalary()));
reduce
//求集合元素只和
Integer result = stream.reduce(0, Integer::sum);
//求最大值
stream.reduce(Integer::max)
//求最小值
stream.reduce(Integer::min)
//求集合元素只和
Integer result = stream.reduce(0, Integer::sum);
//求逻辑求乘机
int result2 = stream.filter(i -> i % 2 == 0).reduce(1, (i, j) -> i * j);
jdk8-collect的更多相关文章
- JAVA常用集合源码解析系列-ArrayList源码解析(基于JDK8)
文章系作者原创,如有转载请注明出处,如有雷同,那就雷同吧~(who care!) 一.写在前面 这是源码分析计划的第一篇,博主准备把一些常用的集合源码过一遍,比如:ArrayList.HashMap及 ...
- JDK8新特性一览
转载自:http://blog.csdn.net/qiubabin/article/details/70256683 官方新特性说明地址 Jdk8新特性.png 下面对几个常用的特性做下重点说明. 一 ...
- JDK8.0新特性
连接转载地址:http://www.2cto.com/kf/201609/544044.html Eclipse: http://aiyiupload.oss-cn-beijing.aliyuncs. ...
- JDK8源码阅读之Collection及相关方法
最近面试总会被问到JDK8中的一些新特性,所以闲下来抽时间看了一下8的源码,目前主要看的是数据结构部分,特此记录一下. 新增函数式接口,实现该接口的可以直接用lambda表达式. default和st ...
- jdk8中关于操作集合的一些新特性,遍历和排序操作
jdk8增加了不少新的东西,在集合操作这块,就有如 lamda表达式,stream,sort,optional等新的类,主要涉及遍历和排序等方面,新特性提升了不少性能,我们开发就是要拥抱新事物,守着老 ...
- JDK8字符串拼接的正确姿势
1. 对列表中的元素进行拼接 以前,对一个列表中的字符串进行拼接时,常见的代码如示例1所示: 代码示例1 List<String> ids = ImmutableList.of(" ...
- jdk8新特性-亮瞎眼的lambda表达式
jdk8之前,尤其是在写GUI程序的事件监听的时候,各种的匿名内部类,大把大把拖沓的代码,程序毫无美感可言!既然Java中一切皆为对象,那么,就类似于某些动态语言一样,函数也可以当成是对象啊!代码块也 ...
- 试水jdk8 stream
jdk8出来日子不短了,jdk11都出来了,不过用的最多的不过是1.5罢了. 今年终于鼓起勇气认真对待它,在18年记录下学习stream,画上一个圆. 先看个图 Java8中有两大最为重要的改变.第一 ...
- jdk8新特性-stream
一.什么是流stream 1.可理解为高级版本的 Iterator 不是集合元素,它不是数据结构并不保存数据,它是有关算法和计算的. 2.单向,不可往复 数据只能遍历一次,遍历过一次后即用尽了,就好比 ...
- jdk8中的StreamAPI
1.实体类 package com.zy.model; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.D ...
随机推荐
- 51Nod——N1082 与7无关的数
https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1082 题目来源: 有道难题 基准时间限制:1 秒 空间限制:13107 ...
- Hadoop ecosystem 生态圈
Cascading: hadoop上面的workflow Sqoop(发音:skup)是一款开源的工具,主要用于在Hadoop(Hive)与传统的数据库(mysql.postgresql...)间进行 ...
- [React] Animate your user interface in React with styled-components and "keyframes"
In this lesson, we learn how to handle CSS keyframe animations in styled-components, via the 'keyfra ...
- JS学习笔记 - fgm练习 - 输入数字求和 正则replace onkeyup事件
<style> body{font-size: 12px;} .outer{ width: 500px; margin: 0 auto; } span{ color: #999; } in ...
- Fiddler--功能简介
Fiddler的基本介绍 Fiddler的官方网站: www.fiddler2.com Fiddler官方网站提供了大量的帮助文档和视频教程, 这是学习Fiddler的最好资料. Fiddler是最 ...
- JavaFX2 - 文本可复制的Label
背景介绍 我的公司和我个人一直都使用JavaFX2来编写client应用程序,同一时候也作为Applet在浏览器中执行. 我们的客户以前拿我们的产品和网页对照,然后向我们提过两个需求: (1) 希望界 ...
- WM_CAP_DRIVER_CONNECT
WM_CAP_DRIVER_CONNECT //ActiveX ---->OnCreate m_pit.Create(IDD_CAM_DIALOG,this); CRect rc; this ...
- opencv播放不了AVI视频的问题
有些avi视频的编码可能不是Cinepak Codec by Radius编码格式的,需要转换成这种格式. 我用的是swf转avi视频,在转变换时----->设置---->AVI视频设置- ...
- Java 线程第三版 第九章 Thread调度 读书笔记
一.Thread调度的概述 import java.util.*; import java.text.*; public class Task implements Runnable { long n ...
- term- xshell VS securecrt
Xshell使用技巧总结 https://www.cnblogs.com/zhangwuji/p/7155575.html SecureCRT与Xshell简单对比 http://ju.outofme ...