当前位置:网站首页>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
边栏推荐
- Youqilin 22.04 lts version officially released | ukui 3.1 opens a new experience
- 2021数学建模国赛一等奖经验总结与分享
- 做数据可视化应该避免的8个误区
- leetcode003--判断一个整数是否为回文数
- Record the ThreadPoolExecutor main thread waiting for sub threads
- Unity3d practical skills - theoretical knowledge base (I)
- Flink's important basics
- How to regulate intestinal flora? Introduction to common natural substances, probiotics and prebiotics
- mysql table 中增加列的SQL语句
- shell wc (统计字符数量)的基本使用
猜你喜欢
Installation and deployment of Flink and wordcount test
Apache Bench(ab 压力测试工具)的安装与使用
Experience summary and sharing of the first prize of 2021 National Mathematical Modeling Competition
补充番外14:cmake实践项目笔记(未完待续4/22)
補:注解(Annotation)
RC低通滤波器的逆系统
Use recyclerview to realize left-right side-by-side classification selection
做数据可视化应该避免的8个误区
AWS EKS 部署要点以及控制台与eksctl创建的差异
[paper reading] [3D target detection] point transformer
随机推荐
Chapter 4 - understanding standard equipment documents, filters and pipelines
QML advanced (IV) - drawing custom controls
Leetcode008 -- implement strstr() function
229. Find mode II
在AWS控制台创建VPC(无图版)
Unity摄像头跟随鼠标旋转
The perfect combination of collaborative process and multi process
mysql ,binlog 日志查询
Unity camera rotation with sliding effect (rotation)
Spark small case - RDD, spark SQL
test
从MySQL数据库迁移到AWS DynamoDB
华为机试--高精度整数加法
协程与多进程的完美结合
General enumeration constant class
Supplément: annotation
leetcode009--用二分查找在数组中搜索目标值
Small volume Schottky diode compatible with nsr20f30nxt5g
第四章 --- 了解标准设备文件、过滤器和管道
Unity攝像頭跟隨鼠標旋轉