当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- C calls SendMessage to refresh the taskbar icon (the icon does not disappear at the end of forcing)
- 意派Epub360丨你想要的H5模板都在这里,电子书、大转盘、红包雨、问卷调查……
- Small program introduction to proficient (2): understand the four important files of small program development
- The essence of transaction and the principle of deadlock
- Git rebase is in trouble. What to do? Waiting line
- 实验一
- 行为型模式之解释器模式
- How to start the hidden preferences in coda 2 on the terminal?
- 实用工具类函数(持续更新)
- Stickinengine architecture 12 communication protocol
猜你喜欢

Zero basis to build a web search engine of its own

【涂鸦物联网足迹】物联网基础介绍篇

2020 database technology conference helps technology upgrade

What is the tensor in tensorflow?

ado.net和asp.net的关系

How does filecoin's economic model and future value support the price of fil currency breaking through thousands

An article taught you to use HTML5 SVG tags

JVM memory allocation - xms128m - xmx512m - XX: permsize = 128M - XX: maxpermsize = 512M

Countdown | 2020 PostgreSQL Asia Conference - agenda arrangement of Chinese sub Forum

ES6 learning notes (2): teach you to play with class inheritance and class objects
随机推荐
STM32F030F4P6兼容灵动微MM32F031F4P6
2020-09-04:函数调用约定了解么?
Qt音视频开发46-视频传输UDP版
Es create a new index database and copy the old index library, practice pro test effective!
This project allows you to quickly learn about a programming language in a few minutes
Zero basis to build a web search engine of its own
Flink's datasource Trilogy: direct API
The essence of transaction and the principle of deadlock
[self taught unity2d legendary game development] map editor
[learning] interface test case writing and testing concerns
Git rebase is in trouble. What to do? Waiting line
Python 100 cases
Using an example to understand the underlying processing mechanism of JS function
Detect certificate expiration script
Epu360: all the H5 templates you want are here, e-book, big turntable, red envelope rain, questionnaire survey
Jenkins installation and deployment process
An article will take you to understand CSS3 fillet knowledge
上海巨微专用蓝牙广播芯片
Road to simple HTML + JS to achieve the most simple game Tetris
image operating system windows cannot be used on this platform