当前位置:网站首页>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
边栏推荐
- 大道至简 html + js 实现最朴实的小游戏俄罗斯方块
- The AI method put forward by China has more and more influence. Tianda et al. Mined the development law of AI from a large number of literatures
- (2) ASP.NET Core3.1 Ocelot routing
- Helping financial technology innovation and development, atfx is at the forefront of the industry
- Building a new generation cloud native data lake with iceberg on kubernetes
- 小游戏云开发入门
- How to hide part of barcode text in barcode generation software
- Even liver three all night, jvm77 high frequency interview questions detailed analysis, this?
- 開源一套極簡的前後端分離專案腳手架
- 2020年数据库技术大会助力技术提升
猜你喜欢
Take you to learn the new methods in Es5
Live broadcast preview | micro service architecture Learning Series live broadcast phase 3
【转发】查看lua中userdata的方法
ES6 learning notes (3): teach you to use js object-oriented thinking to realize the function of adding, deleting, modifying and checking tab column
An article takes you to understand CSS gradient knowledge
如何对数据库账号权限进行精细化管理?
ERD-ONLINE 免费在线数据库建模工具
An article will introduce you to HTML tables and their main attributes
Look! Internet, e-commerce offline big data analysis best practice! (Internet disk link attached)
Outsourcing is really difficult. As an outsourcer, I can't help sighing.
随机推荐
To Lianyun analysis: why is IPFs / filecoin mining so difficult?
Will blockchain be the antidote to the global epidemic accelerating the transformation of Internet enterprises?
An article will take you to understand CSS alignment
事务的隔离级别与所带来的问题
Filecoin has completed a major upgrade and achieved four major project progress!
华为Mate 40 系列搭载HMS有什么亮点?
IPFs rudder filecoin landing at the same time, fil currency price broke a thousand
一路踩坑,被迫聊聊 C# 代码调试技巧和远程调试
如何对数据库账号权限进行精细化管理?
Application of restful API based on MVC
EOS founder BM: what's the difference between UE, UBI and URI?
StickEngine-架构11-消息队列(MessageQueue)
DC-1靶機
Building a new generation cloud native data lake with iceberg on kubernetes
What knowledge do Python automated testing learn?
消息队列(MessageQueue)-分析
An article will introduce you to HTML tables and their main attributes
StickEngine-架构12-通信协议
Look! Internet, e-commerce offline big data analysis best practice! (Internet disk link attached)
[C] (original) step by step teach you to customize the control element - 04, ProgressBar (progress bar)