当前位置:网站首页>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
边栏推荐
- 2020 is coming to an end, special and unforgettable.
- What is the thirty-six plan
- 383. Ransom letter
- 383. 赎金信
- Unity camera rotation with sliding effect (rotation)
- QML进阶(五)-通过粒子模拟系统实现各种炫酷的特效
- 229. 求众数 II
- Leetcode005 -- delete duplicate elements in the array in place
- What's the difference between error and exception
- Detailed explanation of life cycle component of jetpack
猜你喜欢
[timing] empirical evaluation of general convolution and cyclic networks for sequence modeling based on TCN
Ali's ten-year technical experts jointly created the "latest" jetpack compose project combat drill (with demo)
520. Detect capital letters
做数据可视化应该避免的8个误区
MySQL queries users logged in for at least N consecutive days
AWS eks add cluster user or Iam role
补:注解(Annotation)
zynq平台交叉编译器的安装
Supplement: Annotation
Phishing for NFT
随机推荐
Leetcode002 -- inverts the numeric portion of a signed integer
leetcode007--判断字符串中的括号是否匹配
Leetcode004 -- Roman numeral to integer
The perfect combination of collaborative process and multi process
The 14th issue of HMS core discovery reviews the long article | enjoy the silky clip and release the creativity of the video
Spark optimization
数据孤岛是什么?为什么2022年仍然存在数据孤岛?
Microbial neuroimmune axis -- the hope of prevention and treatment of cardiovascular diseases
阿里十年技术专家联合打造“最新”Jetpack Compose项目实战演练(附Demo)
Installation and use of Apache bench (AB pressure test tool)
Leetcode008 -- implement strstr() function
IDE Idea 自动编译 与 On Upate Action 、 On Frame Deactivation 的配置
补充番外14:cmake实践项目笔记(未完待续4/22)
Detailed explanation of life cycle component of jetpack
What is a blocking queue? What is the implementation principle of blocking queue? How to use blocking queue to implement producer consumer model?
Use recyclerview to realize left-right side-by-side classification selection
Recursive call -- Enumeration of permutations
Interaction of diet gut microbiota on cardiovascular disease
The unity camera rotates with the mouse
AWS EKS 部署要点以及控制台与eksctl创建的差异