WeakMap

WeakMap能够使用的场合是

System.gc以后,不能被引用的对象不会在weakMap中存在,例如下面:

 

在a = null;System.gc以后,weakMap中将不会再存在a的键。

 

 

                String a = new String("a");
		String b = new String("b");
		Map weakMap = new WeakHashMap();
		weakMap.put(a, "aaa");
		weakMap.put(b, "bbb");
		a = null;
		System.gc();
		Iterator iterator = weakMap.entrySet().iterator();
		while (iterator.hasNext()) {
			Map.Entry en = (Entry) iterator.next();
			System.out.println(en.getKey() + " : " + en.getValue());
		}
 

你可能感兴趣的:(weak)