-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathWeakHashMapTest.java
More file actions
56 lines (39 loc) · 1.15 KB
/
Copy pathWeakHashMapTest.java
File metadata and controls
56 lines (39 loc) · 1.15 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
package com.inbravo.ref;
import java.util.HashMap;
import java.util.WeakHashMap;
/**
*
* @author amit.dixit
*
*/
public final class WeakHashMapTest {
public static final void main(final String... args) {
normalHashMap(args);
weakHashMap(args);
}
public static final void weakHashMap(final String... args) {
/* Create new weak hash map */
final WeakHashMap<String, String> aMap = new WeakHashMap<String, String>();
/* New strings */
String emp = new String("Vinoth");
final String val = new String("Programmer");
aMap.put(emp, val);
emp = null;
System.gc();
int count = 0;
while (0 != aMap.size()) {
++count;
System.gc();
}
System.out.println("Took " + count + " calls to System.gc() to result in weakHashMap size of : " + aMap.size());
}
public static final void normalHashMap(final String... args) {
final HashMap<String, String> aMap = new HashMap<String, String>();
String emp = new String("Vinoth");
final String val = new String("Programmer");
aMap.put(emp, val);
emp = null;
System.gc();
System.out.println("Size of Map: " + aMap.size());
}
}