当前位置:网站首页>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
边栏推荐
- StickEngine-架构12-通信协议
- The method of realizing high SLO on large scale kubernetes cluster
- 递归、回溯算法常用数学基础公式
- Ronglian completed US $125 million f round financing
- Small program introduction to proficient (2): understand the four important files of small program development
- 如何对数据库账号权限进行精细化管理?
- Summary of front-end interview questions (C, s, s) that front-end engineers need to understand (2)
- ado.net和asp.net的关系
- 事务的本质和死锁的原理
- A small goal in 2019 to become a blog expert of CSDN
猜你喜欢

Filecoin has completed a major upgrade and achieved four major project progress!

【自学unity2d传奇游戏开发】地图编辑器

事务的隔离级别与所带来的问题

Diamond standard

华为云微认证考试简介

Bitcoin once exceeded 14000 US dollars and is about to face the test of the US election

游戏开发中的新手引导与事件管理系统

ERD-ONLINE 免费在线数据库建模工具

CloudQuery V1.2.0 版本发布

EOS founder BM: what's the difference between UE, UBI and URI?
随机推荐
ado.net和asp.net的关系
【转发】查看lua中userdata的方法
Description of phpshe SMS plug-in
Git rebase is in trouble. What to do? Waiting line
An article will take you to understand CSS alignment
华为Mate 40 系列搭载HMS有什么亮点?
统计项目代码行数
文件过多时ls命令为什么会卡住?
【自学unity2d传奇游戏开发】地图编辑器
如何在终端启动Coda 2中隐藏的首选项?
What are the common problems of DTU connection
Building a new generation cloud native data lake with iceberg on kubernetes
What are manufacturing and new automation technologies?
EOS founder BM: what's the difference between UE, UBI and URI?
Flink's datasource Trilogy 2: built in connector
[efficiency optimization] Nani? Memory overflow again?! It's time to sum up the wave!!
The importance of big data application is reflected in all aspects
Unity性能优化整理
大会倒计时|2020 PostgreSQL亚洲大会-中文分论坛议程安排
An article will take you to understand CSS3 fillet knowledge