当前位置:网站首页>list转换map(根据key来拆分list,相同key的value为一个list)
list转换map(根据key来拆分list,相同key的value为一个list)
2020-11-06 21:45:00 【会开车的架构师】
/**
* @description 按照key拆分list
* @data ${DATA}
* @auther
*/
public class MyListTOMap {
public static void main(String[] args) {
UserTest user1 = new UserTest(10,"xiaoming");
UserTest user2 = new UserTest(20,"zhongming");
UserTest user3 = new UserTest(30,"daming");
UserTest user4 = new UserTest(30,"i'mgoal");
List<UserTest> list = new ArrayList<>();
list.add(user1);
list.add(user2);
list.add(user3);
list.add(user4);
Map<Integer,List<UserTest>> mapTest = new HashMap<>();
for (UserTest u: list) {
if(mapTest.containsKey(u.getAge())){
mapTest.get(u.getAge()).add(u);
}else{
List<UserTest> tempList = new ArrayList<>();
tempList.add(u);
mapTest.put(u.getAge(),tempList);
}
}
list.forEach(System.out::println);
System.out.println(mapTest);
}
}
版权声明
本文为[会开车的架构师]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4629483/blog/4529695
边栏推荐
- What are manufacturing and new automation technologies?
- PHP application docking justswap special development kit【 JustSwap.PHP ]
- 2020年数据库技术大会助力技术提升
- An article takes you to understand CSS3 picture border
- 递归、回溯算法常用数学基础公式
- 解决 WPF 绑定集合后数据变动界面却不更新的问题
- To teach you to easily understand the basic usage of Vue codemirror: mainly to achieve code editing, verification prompt, code formatting
- 代码重构之法——方法重构分析
- Axios learning notes (2): easy to understand the use of XHR and how to package simple Axios
- Chainlink brings us election results into blockchain everipedia
猜你喜欢
To teach you to easily understand the basic usage of Vue codemirror: mainly to achieve code editing, verification prompt, code formatting
MongoDB与SQL常用语法对应表
只有1个字节的文件实际占用多少磁盘空间
Outsourcing is really difficult. As an outsourcer, I can't help sighing.
每个大火的“线上狼人杀”平台,都离不开这个新功能
前端未來趨勢之原生API:Web Components
【转发】查看lua中userdata的方法
C#和C/C++混合编程系列5-内存管理之GC协同
GitHub: the foundation of the front end
Unity性能优化整理
随机推荐
2020年数据库技术大会助力技术提升
What knowledge do Python automated testing learn?
How to turn data into assets? Attracting data scientists
How to understand Python iterators and generators?
Live broadcast preview | micro service architecture Learning Series live broadcast phase 3
Analysis of serilog source code -- how to use it
【:: 是什么语法?】
2020年第四届中国 BIM (数字建造)经理高峰论坛即将在杭举办
事务的隔离级别与所带来的问题
html+vue.js 實現分頁可相容IE
From overseas to China, rancher wants to do research on container cloud market
Even liver three all night, jvm77 high frequency interview questions detailed analysis, this?
C# 调用SendMessage刷新任务栏图标(强制结束时图标未消失)
事件监听问题
What course of artificial intelligence? Will it replace human work?
It's time for your financial report to change to a more advanced style -- financial analysis cockpit
一路踩坑,被迫聊聊 C# 代码调试技巧和远程调试
Even liver three all night, jvm77 high frequency interview questions detailed analysis, this?
如何在终端启动Coda 2中隐藏的首选项?
Shh! Is this really good for asynchronous events?