当前位置:网站首页>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
边栏推荐
- 华为Mate 40 系列搭载HMS有什么亮点?
- 2020年第四届中国 BIM (数字建造)经理高峰论坛即将在杭举办
- Summary of front-end performance optimization that every front-end engineer should understand:
- GUI engine evaluation index
- How to get started with new HTML5 (2)
- Elasticsearch Part 6: aggregate statistical query
- An article taught you to use HTML5 SVG tags
- StickEngine-架构11-消息队列(MessageQueue)
- Markdown tricks
- Get twice the result with half the effort: automation without cabinet
猜你喜欢
![PHP application docking justswap special development kit【 JustSwap.PHP ]](/img/3b/00bc81122d330c9d59909994e61027.jpg)
PHP application docking justswap special development kit【 JustSwap.PHP ]

解决 WPF 绑定集合后数据变动界面却不更新的问题

An article taught you to use HTML5 SVG tags

A small goal in 2019 to become a blog expert of CSDN

C + + and C + + programmers are about to be eliminated from the market

Description of phpshe SMS plug-in

面试官: ShardingSphere 学一下吧

Basic usage of Vue codemirror: search function, code folding function, get editor value and verify in time

Pn8162 20W PD fast charging chip, PD fast charging charger scheme

Look! Internet, e-commerce offline big data analysis best practice! (Internet disk link attached)
随机推荐
A small goal in 2019 to become a blog expert of CSDN
C + + and C + + programmers are about to be eliminated from the market
Basic usage of Vue codemirror: search function, code folding function, get editor value and verify in time
What is the meaning of sector sealing of filecoin mining machine since the main network of filecoin was put online
理解格式化原理
Even liver three all night, jvm77 high frequency interview questions detailed analysis, this?
意派Epub360丨你想要的H5模板都在这里,电子书、大转盘、红包雨、问卷调查……
Get twice the result with half the effort: automation without cabinet
Staying up late summarizes the key points of report automation, data visualization and mining, which is different from what you think
An article takes you to understand CSS gradient knowledge
視覺滾動[反差美]
大会倒计时|2020 PostgreSQL亚洲大会-中文分论坛议程安排
image operating system windows cannot be used on this platform
面试官: ShardingSphere 学一下吧
Summary of front-end performance optimization that every front-end engineer should understand:
文件过多时ls命令为什么会卡住?
ES6 learning notes (3): teach you to use js object-oriented thinking to realize the function of adding, deleting, modifying and checking tab column
Introduction to Google software testing
What knowledge do Python automated testing learn?
每个大火的“线上狼人杀”平台,都离不开这个新功能