-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeakHashTesy.java
More file actions
81 lines (61 loc) · 1.64 KB
/
Copy pathWeakHashTesy.java
File metadata and controls
81 lines (61 loc) · 1.64 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package set;
import java.util.Iterator;
import java.util.Map;
import java.util.WeakHashMap;
/**
* Created by helmeter on 4/16/16.
*/
public class WeakHashTesy {
public static void main(String[] args) {
WeakHashMap<AA, People1> weakMap1 = new WeakHashMap<AA, People1>();
String b = new String("louhang1");
AA a = new AA(b);
BB bb = new BB(a);
People1 p1 = new People1(bb);
weakMap1.put(p1.getB().getAA(), p1);
// p1.getB().setAA(null);// 去除对象a的强引用
//a = null;// 去除对象a的强引用,并让垃圾收集器回收AA对象在堆中的内存
System.gc();
Iterator i = weakMap1.entrySet().iterator();
while (i.hasNext()) {
Map.Entry en = (Map.Entry) i.next();
System.out.println("weakMap:" + en.getKey() + ":" + en.getValue());
}
}
}
class AA {
private String a;
public AA(String a) {
this.a = a;
}
public String getA() {
return a;
}
public void setA(String a) {
this.a = a;
}
}
class BB {
private AA a;
public BB(AA a) {
this.a = a;
}
public AA getAA() {
return a;
}
public void setAA(AA a) {
this.a = a;
}
}
class People1 {
private BB b;
public People1(BB b) {
this.b = b;
}
public BB getB() {
return b;
}
public void setB(BB b) {
this.b = b;
}
}