当前位置:网站首页>List to map (split the list according to the key, and the value of the same key is a list)
List to map (split the list according to the key, and the value of the same key is a list)
2020-11-06 21:45:00 【Architects who can drive】
/**
* @description according to key Split 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);
}
}
版权声明
本文为[Architects who can drive]所创,转载请带上原文链接,感谢
边栏推荐
- ado.net和asp.net的关系
- The isolation level of transaction and its problems
- ES中删除索引的mapping字段时应该考虑的点
- Python basic variable type -- list analysis
- An article takes you to understand CSS gradient knowledge
- How does cglib implement multiple agents?
- Jenkins installation and deployment process
- The essence of transaction and the principle of deadlock
- Message queue - Analysis
- window系统 本机查找端口号占用方法
猜你喜欢

Detect certificate expiration script

Small program introduction to proficient (2): understand the four important files of small program development

预留电池接口,内置充放电电路及电量计,迅为助力轻松搞定手持应用

打工人好物——磨炼钢铁意志就要这样高效的电脑

Method of code refactoring -- Analysis of method refactoring

移动端像素适配方案

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

To Lianyun analysis: why is IPFs / filecoin mining so difficult?

C and C / C + + mixed programming series 5 - GC collaboration of memory management

An article will take you to understand CSS alignment
随机推荐
All the way, I was forced to talk about C code debugging skills and remote debugging
To Lianyun analysis: why is IPFs / filecoin mining so difficult?
What is the tensor in tensorflow?
Using iceberg on kubernetes to create a new generation of cloud original data Lake
An article takes you to understand CSS gradient knowledge
mongo 用户权限 登录指令
Zhou Jie: database system of East China Normal University
1万辆!理想汽车召回全部缺陷车:已发生事故97起,亏损将扩大
Bitcoin once exceeded 14000 US dollars and is about to face the test of the US election
【涂鸦物联网足迹】物联网基础介绍篇
image operating system windows cannot be used on this platform
An article will take you to understand SVG gradient knowledge
Understanding formatting principles
Can you do it with only six characters?
Code generator plug-in and creator preform file analysis
The memorandum model of behavior model
Es create a new index database and copy the old index library, practice pro test effective!
Git rebase is in trouble. What to do? Waiting line
实验一
The native API of the future trend of the front end: web components