当前位置:网站首页>List< Map> Replication: light copy and deep copy
List< Map> Replication: light copy and deep copy
2022-04-23 04:43:00 【Mount MONI】
Shallow copy :
list A Shallow copy to list B,java An array of the same contents in points to the same address , That is, after shallow copy A And B Point to the same address . The result is , change B It will change at the same time A, Because of change B Change B The content of the address pointed to , because A Also point to the same address , therefore A And B Change together .
initialization List l1
List<Map<String, Object>> l1 = new ArrayList<>();
Map<String, Object> m1 = new HashMap<>();
Map<String, Object> m2 = new HashMap<>();
Map<String, Object> m3 = new HashMap<>();
Map<String, Object> m4 = new HashMap<>();
m1.put("name", "zhangsan");
m1.put("value", 100);
m2.put("name", "lisi");
m2.put("value", 200);
m3.put("name", "xiaoming");
m3.put("value", 300);
m4.put("name", "xiaohong");
m4.put("value", 400);
l1.add(m1);
l1.add(m2);
l1.add(m3);
l1.add(m4);
1. Traversal loop replication
List<Map<String, Object>> shallowCopyList = new ArrayList<>(l1.size());
for (Map<String, Object> map : l1) {
shallowCopyList.add(map);
}
2. Use List Implementation class construction method
List<Map<String, Object>> shallowCopyList = new ArrayList<>(l1);
3. Use list.addAll() Method
List<Map<String, Object>> shallowCopyList = new ArrayList<>();
shallowCopyList.addAll(l1);
Deep copy :
Deep copy is to A Copy to B At the same time , to B Create a new address , Then add the address A The content of is delivered to the address B.ListA And ListB The content is consistent , However, due to the different addresses pointed to , So change is not affected by each other .
List<Map<String, Object>> deepCopyList = new ArrayList<>(l1.size());
for (Map<String, Object> map : l1) {
deepCopyList.add((Map<String, Object>) SerializationUtils.clone((Serializable) map));
}
deepCopyList.addAll(deepCopyList);
deepCopyList.get(0).put("value", 10086);
Results output :
primary List :[{"name":"zhangsan","value":100},{"name":"lisi","value":200},{"name":"xiaoming","value":300},{"name":"xiaohong","value":400}]
new List :[{"value":10086,"name":"zhangsan"},{"value":200,"name":"lisi"},{"value":300,"name":"xiaoming"},{"value":400,"name":"xiaohong"}]
版权声明
本文为[Mount MONI]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220558292082.html
边栏推荐
- 用D435i录制自己的数据集运行ORBslam2并构建稠密点云
- Recommended scheme of national manufactured electronic components
- Microbial neuroimmune axis -- the hope of prevention and treatment of cardiovascular diseases
- 383. 赎金信
- Redis command Encyclopedia
- 520. Detect capital letters
- RC低通滤波器的逆系统
- [paper reading] [3D object detection] voxel transformer for 3D object detection
- The perfect combination of collaborative process and multi process
- 重剑无锋,大巧不工
猜你喜欢

win10, mysql-8.0.26-winx64. Zip installation

Coinbase: basic knowledge, facts and statistics about cross chain bridge

Druid -- JDBC tool class case

针对NFT的网络钓鱼

基于英飞凌MCU GTM模块的无刷电机驱动方案开源啦

Flink's important basics

Small volume Schottky diode compatible with nsr20f30nxt5g

The 14th issue of HMS core discovery reviews the long article | enjoy the silky clip and release the creativity of the video

Record your own dataset with d435i, run orbslam2 and build a dense point cloud
![Luogu p1858 [multi person knapsack] (knapsack seeking the top k optimal solution)](/img/2e/3313e563ac4f54057e359941a45098.png)
Luogu p1858 [multi person knapsack] (knapsack seeking the top k optimal solution)
随机推荐
Detailed explanation of life cycle component of jetpack
IDE Idea 自动编译 与 On Upate Action 、 On Frame Deactivation 的配置
Redis 命令大全
Go反射法则
Improving 3D object detection with channel wise transformer
zynq平台交叉编译器的安装
Huawei machine test -- high precision integer addition
Luogu p1858 [multi person knapsack] (knapsack seeking the top k optimal solution)
MySQL queries users logged in for at least N consecutive days
做数据可视化应该避免的8个误区
What is a data island? Why is there still a data island in 2022?
What is a blocking queue? What is the implementation principle of blocking queue? How to use blocking queue to implement producer consumer model?
Open the past and let's start over.
What is the meaning of load balancing
Innovative practice of short video content understanding and generation technology in meituan
Unity RawImage背景无缝连接移动
What's the difference between error and exception
shell wc (统计字符数量)的基本使用
Mysql, binlog log query
Redis command Encyclopedia