我创建了一个 WeakHashMap,代码如下:
WeakHashMap<Employee,String> map = new WeakHashMap<Employee,String>();
map.put(emp,"hello");
如果emp是一个Employee对象。现在,如果我将emp设置为null或不再引用emp对象,那么WeakHashMap中的条目是否会被删除,即Map的大小是否为零?
而在HashMap的情况下是否反之亦然?
我的WeakHashMap理解正确吗?
我创建了一个 WeakHashMap,代码如下:
WeakHashMap<Employee,String> map = new WeakHashMap<Employee,String>();
map.put(emp,"hello");
如果emp是一个Employee对象。现在,如果我将emp设置为null或不再引用emp对象,那么WeakHashMap中的条目是否会被删除,即Map的大小是否为零?
而在HashMap的情况下是否反之亦然?
我的WeakHashMap理解正确吗?
一个非常简单的例子,来说明已经说过的内容:
import java.util.WeakHashMap;
public class WeakHashMapDemo {
public static void main(String[] args) {
// -- Fill a weak hash map with one entry
WeakHashMap<Data, String> map = new WeakHashMap<Data, String>();
Data someDataObject = new Data("foo");
map.put(someDataObject, someDataObject.value);
System.out.println("map contains someDataObject ? " + map.containsKey(someDataObject));
// -- now make someDataObject elligible for garbage collection...
someDataObject = null;
for (int i = 0; i < 10000; i++) {
if (map.size() != 0) {
System.out.println("At iteration " + i + " the map still holds the reference on someDataObject");
} else {
System.out.println("somDataObject has finally been garbage collected at iteration " + i + ", hence the map is now empty");
break;
}
}
}
static class Data {
String value;
Data(String value) {
this.value = value;
}
}
}
输出:
map contains someDataObject ? true
...
At iteration 6216 the map still holds the reference on someDataObject
At iteration 6217 the map still holds the reference on someDataObject
At iteration 6218 the map still holds the reference on someDataObject
somDataObject has finally been garbage collected at iteration 6219, hence the map is now empty
我运行了示例代码,以了解HashMap
和WeakHashMap
之间的区别。
Map hashMap= new HashMap();
Map weakHashMap = new WeakHashMap();
String keyHashMap = new String("keyHashMap");
String keyWeakHashMap = new String("keyWeakHashMap");
hashMap.put(keyHashMap, "helloHash");
weakHashMap.put(keyWeakHashMap, "helloWeakHash");
System.out.println("Before: hash map value:"+hashMap.get("keyHashMap")+" and weak hash map value:"+weakHashMap.get("keyWeakHashMap"));
keyHashMap = null;
keyWeakHashMap = null;
System.gc();
System.out.println("After: hash map value:"+hashMap.get("keyHashMap")+" and weak hash map value:"+weakHashMap.get("keyWeakHashMap"));
输出结果将为:
Before: hash map value:helloHash and weak hash map value:helloWeakHash
After: hash map value:helloHash and weak hash map value:null
那么在HashMap的情况下会反过来吗?
从WeakHashMap中删除条目不会影响程序中的任何其他引用。
WeakHashMap示例:
Map map = new WeakHashMap();
Foo foo = new Foo();
map.put(foo, "bar");
foo=null; // strong refrence is removed and object is available for garbage collection.
HashMap示例:
Map map = new HashMap();
Foo foo = new Foo();
map.put(foo, "bar");
foo=null; // even though the reference is nullified object will not garbage collected because map is having Strong refrence.
wm.put(person, person.getFirstName());
person = null;
System.gc();
System.out.println("Hash Map :" + wm.toString());
输出:哈希映射:{test.Person@12dacd1=John}
与HashMap相比,WeakHashMap是一种会在键没有内存引用时立即删除其条目的映射。例如,如果一个WeakHashMap的键如下所示为Person类,并且如果Person对象设置为null,则现在如果您执行map.get(Person),我们将得到null,因为该键没有引用(或者说是弱可达的)。
wm.put(person, person.getFirstName());
person = null;
System.gc();
System.out.println("Weak Hash Map :" + wm.toString());
Java中的引用是指向内存中创建对象的内存地址。在WeakHashMap中,使用了弱引用的概念。
一旦你在Java中创建一个对象并将其分配给某个变量,则它就变得强引用了。
弱引用对象可以类比于没有内存引用的对象,即它现在可以被垃圾回收器回收。