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 ...
随机推荐
- ORACLE10g R2【RAC+ASM→单实例FS】
ORACLE10g R2[RAC+ASM→单实例FS] 10g R2 RAC+ASMà单实例FS的DG,建议禁用OMF. 本演示案例所用环境: primary standby OS Hostnam ...
- 忍者无敌-实例解说Cocos2d-x瓦片地图
实例比較简单,如图所看到的,地图上有一个忍者精灵,玩家点击他周围的上.下.左.右,他能够向这个方向行走. 当他遇到障碍物后是无法穿越的,障碍物是除了草地以为部分,包含了:树.山.河流等. 忍者实例地图 ...
- Javascript和jquery事件--鼠标事件的小结
1.鼠标事件的主要事件应该是mouseup, mousedown, mousewheel, mousemove, mouseover, moveout. <1>其中mouseup和mous ...
- MyEclipse中 使用svn更新jar包 出现 svn cleanup failed–previous operation has not finished; run cleanup if it was interrupted 导致整个svn异常处理
svn cleanup failed–previous operation has not finished; run cleanup if it was interrupted 2014-07-02 ...
- php 如何写一个自己项目的安装程序
版权声明:此篇文章只是用作笔记,如果版权冲突,请邮件通知一下(15201155501@163.com) https://blog.csdn.net/shenpengchao/article/detai ...
- 魔兽争霸war3心得体会(一):UD的冰甲蜘蛛流
玩war3好几年了,之前都是打打电脑,随便玩玩的.刚刚在浩方等平台上和人玩的时候,各种被虐,很难赢一局.从去年开始,才认真玩.思考下各种战术. 最初,使用的是兽族orc,后来觉得兽族不够厉害,玩到对战 ...
- Envelope
IEnvelope Interface Provides access to methods and properties of envelopes. Note: the IEnvelope inte ...
- Html表单中遇到的问题
原文 https://www.jianshu.com/p/4466b8294007 大纲 1.表单提交的方式GET和POST的区别 2.js无法对input的file类型的值进行赋值 3.js获取in ...
- POJ - 2286 - The Rotation Game (IDA*)
IDA*算法,即迭代加深的A*算法.实际上就是迭代加深+DFS+估价函数 题目传送:The Rotation Game AC代码: #include <map> #include < ...
- CISCO - 查找命令行
http://www.cisco.com/c/en/us/support/web/tools/help/command_search_best_practices.html Support Best ...