Java8中list转map
第一种: 取list中某2个字段作为Map的K,V
public Map<Long, String> getIdNameMap(List<Account> accounts) {
return accounts.stream().collect(Collectors.toMap(Account::getId, Account::getUsername));
}
第二种:将id和实体Bean做为K,V
public Map<Long, Account> getIdAccountMap(List<Account> accounts) {
return accounts.stream().collect(Collectors.toMap(Account::getId, account -> account));
}
或者这样写:
public Map<Long, Account> getIdAccountMap(List<Account> accounts) {
return accounts.stream().collect(Collectors.toMap(Account::getId, Function.identity()));
}
account -> account是一个返回本身的lambda表达式,后面的使用Function接口中的一个默认方法代替,使整个方法更简洁优雅。
第三种: key存在重复记录时处理
public Map<String, Account> getNameAccountMap(List<Account> accounts) {
return accounts.stream().collect(Collectors.toMap(Account::getUsername, Function.identity(), (key1, key2) -> key2));
}
如果使用第一种方法会出错,所以这里只是简单的使用后者覆盖前者来解决key重复问题。
第四种: 使用某个具体的Map类来保存,如保存时使用LinkedHashMap
public Map<String, Account> getNameAccountMap(List<Account> accounts) {
return accounts.stream().collect(Collectors.toMap(Account::getUsername, Function.identity(), (key1, key2) -> key2, LinkedHashMap::new));
}
第五种: List<Object>转List<String,Map<String, String>>
类似采购订单 id,对应明细记录。
public Map<String,List<MCode>> getCodeListMap(){
if(CollectionUtils.isEmpty(codeListMap)){
List<MCode> codeList = this.getCodeList();
Set<String> keySet = codeList.stream().map(code -> code.getCodeKbn()).collect(Collectors.toSet());
Iterator<String> it = keySet.iterator();
while(it.hasNext()) {
String key = it.next();
codeListMap.put(key, codeList.stream().filter(code -> code.getCodeKbn().equals(key)).collect(Collectors.toList()));
}
}
return codeListMap;
}
Java8中list转map的更多相关文章
- 【转】Java8中list转map方法总结
https://blog.csdn.net/zlj1217/article/details/81611834 背景在最近的工作开发之中,慢慢习惯了很多Java8中的Stream的用法,很方便而且也可以 ...
- java8中stream的map和flatmap的理解
转自https://blog.csdn.net/wynjauu/article/details/78741093 假如我们有这样一个需求给定单词列表["Hello","W ...
- java8中map的meger方法的使用
java8中map有一个merge方法使用示例: /** * 打印出包含号码集的label的集合 * * @param args */ public static void main(String[] ...
- java8中的map和reduce
java8中的map和reduce 标签: java8函数式mapreduce 2014-06-19 19:14 10330人阅读 评论(4) 收藏 举报 分类: java(47) FP(2) ...
- Java8中Map的遍历方式总结
在这篇文章中,我将对Map的遍历方式做一个对比和总结,将分别从JAVA8之前和JAVA8做一个遍历方式的对比,亲测可行. public class LambdaMap { private Map< ...
- Java8中那些方便又实用的Map函数
原创:扣钉日记(微信公众号ID:codelogs),欢迎分享,转载请保留出处. 简介 java8之后,常用的Map接口中添加了一些非常实用的函数,可以大大简化一些特定场景的代码编写,提升代码可读性,一 ...
- java8 中的时间和数据的变化
java8除了lambda表达式之外还对时间和数组这两块常用API做想应调整, Stream 有几个常用函数: store 排序 (a,b)-> a.compareTo(b) 排出来的结果是正 ...
- Java8中Lambda表达式的10个例子
Java8中Lambda表达式的10个例子 例1 用Lambda表达式实现Runnable接口 //Before Java 8: new Thread(new Runnable() { @Overri ...
- 初探Java8中的HashMap(转)
HashMap是我们最常用的集合之一,同时Java8也提升了HashMap的性能.本着学习的原则,在这探讨一下HashMap. 原理 简单讲解下HashMap的原理:HashMap基于Hash算法,我 ...
随机推荐
- 常用的代码之一:用StopWatch计算代码运行花费的时间。
先引用Diagnostics using System.Diagnostics; 然后: Stopwatch stopWatch = new Stopwatch(); stopWatch.Start( ...
- php分享二十六:支付系统设计
一个典型PHP支付系统的设计与实现 参考:blog.sina.com.cn/s/blog_81f6205801017ec8.html 微信支付开发: http://www.cnblogs.com/tx ...
- idea自动编译
idea修改后台代码自动编译 On 'update' action = Update classes and resources On frame deactivation = Update clas ...
- Kinect v2 记录
最多可同时识别跟踪 6 人,每人可识别到 25 个关节数据.可以根据上身 10 个关节数据来判断坐姿状态. 物理极限识别范围:0.5m – 4.5m,最佳识别范围:0.8m – 3.5m. 深度数据可 ...
- error: unpacking of archive failed on file /usr/sbin/zabbix_agent;592e5bc3: cpio: open
# lsattr /usr/ ----------I--e- /usr/lib64 ----------I--e- /usr/bin -------------e- /usr/libexec ---- ...
- HTML5学习笔记(十八):闭包
高阶函数 JavaScript的函数其实都指向某个变量.既然变量可以指向函数,函数的参数能接收变量,那么一个函数就可以接收另一个函数作为参数,也可以返回一个函数,这种函数就称之为高阶函数. 函数作为参 ...
- dom4j: 用dom4j生成xml后第二行空行的问题
只需要指定: format.setNewLineAfterDeclaration(false); 即可. 例: OutputFormat format = OutputFormat.createPre ...
- Mysql 管理和备份
mysqladmin用于管理MySQL服务器的客户端,mysqladmin执行管理操作的客户程序,可以用它来创建或删除数据库,重载授权表,将表刷新到硬盘上,以及重新打开日志文件,检索版本.进程,以及服 ...
- http.ResponseWriter的Flush
func handle(res http.ResponseWriter, req *http.Request) { fmt.Fprintf(res, "sending first line ...
- hive外部表删除遇到的一个坑
hive外部表删除遇到的一个坑 操作步骤 创建某个表(create external table xxx location xxx) 插入数据(insert xxx select xxx from x ...