当前位置:网站首页>Use map set or list set to store list set
Use map set or list set to store list set
2022-06-21 13:30:00 【Yield & Allure】
1. use list Collection storage list
Be careful :
Every time List<List> Add list when , Should be re opened in memory list Space is convenient for storing new data to list in
List<List> Deposit list Type of data to use add(int i,list) Method, not add(list), In this way, every time new One list When the collection ,List<List> Only one data is stored , Previous list The data will be overwritten ( still list The problem of pointing in the opened memory ), Only the data of the last cycle is output . So it is necessary to specify where the data should be stored each time , Use add(int i,list) Method .
To give list When storing data , Every time new One list, No new Words list Only one is opened in memory list object , Every time you put data in it ,list All point to this object . So to achieve each cycle has a different list when , Create a new one in memory under the loop list
public static void main(String[] args) {
List<List<User>> list = new ArrayList<>();
for (int i = 0; i < 2; i++) {
List<User> list1 = new ArrayList<>();
list1.add(new User("1","zhangsan"));
list1.add(new User("2","lisi"));
list1.add(new User("3","wangwu"));
list.add(list1);
}
System.out.println("list: "+list);
}Running results

It can't be written like this
public static void main(String[] args) {
List<List<User>> list = new ArrayList<>();
List<User> list1 = new ArrayList<>();
for (int i = 0; i < 2; i++) {
list1.add(new User("1","zhangsan"));
list1.add(new User("2","lisi"));
list1.add(new User("3","wangwu"));
list.add(list1);
}
System.out.println("list: "+list);
} Otherwise, the result is :
The above problems will occur , Each time according to the defined cycle add Add after ( Will cause duplicate data )
2. use map Collective storage list
// When adding multiple values :
public static void main(String[] args) {
List<List<User>> list = new ArrayList<>();
Map<String,List<User>> map = new HashMap<>();
for (int i = 0; i < 2; i++) {
List<User> list2 = new ArrayList<>();
list2.add(new User("1","zhangsan"));
list2.add(new User("2","lisi"));
list2.add(new User("3","wangwu"));
map.put("list"+i,list2);
}
}
// When you add a list When the collection :
public static void main(String[] args) {
List<List<User>> list = new ArrayList<>();
Map<String,List<User>> map = new HashMap<>();
List<User> list2 = new ArrayList<>();
list2.add(new User("1","zhangsan"));
list2.add(new User("2","lisi"));
list2.add(new User("3","wangwu"));
map.put("list"+i,list2);
}
Multiple list The result of the value

One list The result of the value

边栏推荐
- uva11995
- 4. procedure flow structure
- Kubernetes' fast practice and core principle analysis
- Is the micro securities account opening regular and safe? How to open an account?
- 2. reference
- 如何使用搜索引擎?
- 3D slicer import label and view label
- PingCAP 入选 2022 Gartner 云数据库“客户之声”,获评“卓越表现者”最高分
- Eureka's timedsupersortask class (periodic task with automatic interval adjustment)
- 618 Nuggets digital collection? Burberry and other luxury brands fight against metauniverse
猜你喜欢

Summary of the latest remote deployment O & M tools

Memcached(高性能内存对象缓存)

Kubernetes快速实战与核心原理剖析

塔米狗 热门资讯:厦门产权交易中心再创第一次!

17 commonly used o & M monitoring systems

Kubernets Rapid Practical fighting and Core Principle Analysis

618 Nuggets digital collection? Burberry and other luxury brands fight against metauniverse

使用seaborn绘制热图

这不会又是一个Go的BUG吧?

服务治理的工作内容
随机推荐
Interpretation of tamigou project: 34% equity transfer of Jining Huayuan Project Management Co., Ltd
Turn to the countdown for coupon issuance! First look at the rules of interstellar pocket donation
What is Devops in an article?
实践 DevOps 时,可能面临的六大挑战
uva11729
Apache ShardingSphere 5.1.2 发布|全新驱动 API + 云原生部署,打造高性能数据网关
Test the interface between app and wechat applet
1. memory partition model
SCCM creates a client collection based on the installed app and periodically pushes application updates
Eureka's timedsupersortask class (periodic task with automatic interval adjustment)
咨询:微证券是什么证券公司,开户安全吗?
7. pointer
5000字解析:实战化场景下的容器安全攻防之道
17 commonly used o & M monitoring systems
Is the live interactive function of the applet running in the app?
8 most common SQL error usages
基于STM32电压检测和电流检测
hands-on-data-analysis 第二单元 第四节数据可视化
How to write test cases
Qinglong panel, JD timed task library, script library