Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
56 lines (47 loc) · 1.72 KB

File metadata and controls

56 lines (47 loc) · 1.72 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package pattern.clone;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* Created by yahier on 12/19/18.
* 原型模式 Cloneable接口和clone方法一点关联都没有的
*/
public class CloneMain {
public static void main(String[] args) {
Map<String, PointBean> map = new HashMap<>();
map.put("1", new PointBean("1123"));
map.put("2", new PointBean("2123"));
map.put("3", new PointBean("3123"));
map.put("4", new PointBean("4123"));
map.put("5", new PointBean("5123"));
Map cloneMap = getClonePointMap(map);
cloneMap.put("6", new PointBean("6123"));
showMapData("原数据", map);
showMapData("副本数据", cloneMap);
}
private static HashMap getClonePointMap(Map map) {
HashMap clone = new HashMap<>();
if (map != null) {
Iterator iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
String key = (String) entry.getKey();
PointBean pointBean = (PointBean) entry.getValue();
if (pointBean != null) {
//遍历map并将克隆对象放到新的map中
clone.put(key, pointBean.clone());
} else {
clone.put(key, null);
}
}
}
return clone;
}
private static void showMapData(String tag, Map<String, PointBean> map) {
Collection<PointBean> collections = map.values();
for (PointBean bean : collections) {
System.out.println(tag + " " + bean.getPointName());
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.