当前位置:网站首页>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
边栏推荐
- An article takes you to understand CSS pagination examples
- 事务的本质和死锁的原理
- ES6 learning notes (5): easy to understand ES6's built-in extension objects
- 行为型模式之备忘录模式
- Introduction to the structure of PDF417 bar code system
- 小游戏云开发入门
- How to understand Python iterators and generators?
- Outsourcing is really difficult. As an outsourcer, I can't help sighing.
- Share with Lianyun: is IPFs / filecoin worth investing in?
- 大会倒计时|2020 PostgreSQL亚洲大会-中文分论坛议程安排
猜你喜欢

面试官: ShardingSphere 学一下吧

The importance of big data application is reflected in all aspects

IPFs rudder filecoin landing at the same time, fil currency price broke a thousand

华为Mate 40 系列搭载HMS有什么亮点?

Will blockchain be the antidote to the global epidemic accelerating the transformation of Internet enterprises?

代码生成器插件与Creator预制体文件解析

Look! Internet, e-commerce offline big data analysis best practice! (Internet disk link attached)

Take you to learn the new methods in Es5

What course of artificial intelligence? Will it replace human work?

What is the meaning of sector sealing of filecoin mining machine since the main network of filecoin was put online
随机推荐
Get twice the result with half the effort: automation without cabinet
Helping financial technology innovation and development, atfx is at the forefront of the industry
大数据处理黑科技:揭秘PB级数仓GaussDB(DWS) 并行计算技术
GitHub: the foundation of the front end
An article will introduce you to CSS3 background knowledge
Introduction to the structure of PDF417 bar code system
事务的本质和死锁的原理
Introduction to Google software testing
The importance of big data application is reflected in all aspects
An article taught you to use HTML5 SVG tags
MongoDB与SQL常用语法对应表
To Lianyun analysis: why is IPFs / filecoin mining so difficult?
Small program introduction to proficient (2): understand the four important files of small program development
image operating system windows cannot be used on this platform
Markdown tricks
How to hide part of barcode text in barcode generation software
[efficiency optimization] Nani? Memory overflow again?! It's time to sum up the wave!!
StickEngine-架构12-通信协议
Staying up late summarizes the key points of report automation, data visualization and mining, which is different from what you think
Take you to learn the new methods in Es5