当前位置:网站首页>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
边栏推荐
- The importance of big data application is reflected in all aspects
- ERD-ONLINE 免费在线数据库建模工具
- What course of artificial intelligence? Will it replace human work?
- hdu3974 Assign the task線段樹 dfs序
- How to turn data into assets? Attracting data scientists
- Even liver three all night, jvm77 high frequency interview questions detailed analysis, this?
- Network security engineer Demo: the original * * is to get your computer administrator rights! [maintain]
- image operating system windows cannot be used on this platform
- An article will introduce you to CSS3 background knowledge
- es创建新的索引库并拷贝旧的索引库 实践亲测有效!
猜你喜欢
How about small and medium-sized enterprises choose shared office?
C + + and C + + programmers are about to be eliminated from the market
大会倒计时|2020 PostgreSQL亚洲大会-中文分论坛议程安排
如何在终端启动Coda 2中隐藏的首选项?
An article takes you to understand CSS3 picture border
给字节的学姐讲如何准备“系统设计面试”
每个大火的“线上狼人杀”平台,都离不开这个新功能
Kubernetes and OAM to build a unified, standardized application management platform knowledge! (Internet disk link attached)
【自学unity2d传奇游戏开发】如何让角色动起来
CloudQuery V1.2.0 版本发布
随机推荐
Shh! Is this really good for asynchronous events?
How to play sortable JS vuedraggable to realize nested drag function of forms
Xmppmini project details: step by step from the principle of practical XMPP technology development 4. String decoding secrets and message package
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
It's time for your financial report to change to a more advanced style -- financial analysis cockpit
Get twice the result with half the effort: automation without cabinet
【自学unity2d传奇游戏开发】地图编辑器
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?
In depth to uncover the bottom layer of garbage collection, this time let you understand her thoroughly
ES6 learning notes (3): teach you to use js object-oriented thinking to realize the function of adding, deleting, modifying and checking tab column
ado.net和asp.net的关系
Git rebase is in trouble. What to do? Waiting line
The method of realizing high SLO on large scale kubernetes cluster
大数据处理黑科技:揭秘PB级数仓GaussDB(DWS) 并行计算技术
GitHub: the foundation of the front end
Basic principle and application of iptables
A small goal in 2019 to become a blog expert of CSDN
如何对数据库账号权限进行精细化管理?
游戏主题音乐对游戏的作用