当前位置:网站首页>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
边栏推荐
- ado.net和asp.net的关系
- 意外的元素..所需元素..
- What are the common problems of DTU connection
- Ronglian completed US $125 million f round financing
- Try to build my mall from scratch (2): use JWT to protect our information security and perfect swagger configuration
- Details of dapr implementing distributed stateful service
- 检测证书过期脚本
- An article will take you to understand CSS alignment
- What is the meaning of sector sealing of filecoin mining machine since the main network of filecoin was put online
- Digital city responds to relevant national policies and vigorously develops the construction of digital twin platform
猜你喜欢
前端未來趨勢之原生API:Web Components
It's time for your financial report to change to a more advanced style -- financial analysis cockpit
【自学unity2d传奇游戏开发】地图编辑器
ERD-ONLINE 免费在线数据库建模工具
How to hide part of barcode text in barcode generation software
The method of realizing high SLO on large scale kubernetes cluster
2020年数据库技术大会助力技术提升
An article will take you to understand SVG gradient knowledge
事务的隔离级别与所带来的问题
大数据处理黑科技:揭秘PB级数仓GaussDB(DWS) 并行计算技术
随机推荐
Network programming NiO: Bio and NiO
Introduction to Google software testing
Some operations kept in mind by the front end foundation GitHub warehouse management
华为Mate 40 系列搭载HMS有什么亮点?
hdu3974 Assign the task線段樹 dfs序
FastThreadLocal 是什么鬼?吊打 ThreadLocal 的存在!!
An article takes you to understand CSS gradient knowledge
【字节跳动 秋招岗位开放啦】Ohayoo!放学别走,我想约你做游戏!!!
What are manufacturing and new automation technologies?
如何对数据库账号权限进行精细化管理?
es创建新的索引库并拷贝旧的索引库 实践亲测有效!
html+vue.js 實現分頁可相容IE
How to turn data into assets? Attracting data scientists
IPFs rudder filecoin landing at the same time, fil currency price broke a thousand
2020年第四届中国 BIM (数字建造)经理高峰论坛即将在杭举办
前端未來趨勢之原生API:Web Components
【ElasticSearch搜索引擎】
Outsourcing is really difficult. As an outsourcer, I can't help sighing.
Read the advantages of Wi Fi 6 over Wi Fi 5 in 3 minutes
To Lianyun analysis: why is IPFs / filecoin mining so difficult?