سؤال

According to Java doc for weakhashmap:

"This class is intended primarily for use with key objects whose equals methods test for object identity using the == operator. Once such a key is discarded it can never be recreated, so it is impossible to do a lookup of that key in a WeakHashMap"

Does this mean that if we use an objectA as key for entry 0 in the weakhashmap, and later we remove that entry testMapHashWeak.remove(objectA); we can not use the same key objectA for another entry? Cause I have made an small test and I can do it:

    public void test4WeakHashMap(WeakHashMap<B, String> testMapHashWeak) {
    B objectB = new B();
    String sTest = "hola";
    System.out.println("1st time - key&value inserted ->"+objectB+","+sTest);
    testMapHashWeak.put(objectB, sTest);
    System.out.println("Get element 1st time-> "+testMapHashWeak.get(objectB));
    testMapHashWeak.remove(objectB);
    //Insert 2nd time
    System.out.println("2st time - key&value inserted ->"+objectB+","+sTest);
    testMapHashWeak.put(objectB, sTest);        
    System.out.println("Get element 2nd time-> "+testMapHashWeak.get(objectB));
}

Being the output:

1st time - key&value inserted ->B@634e3372,hola
Get element 1st time-> hola
2st time - key&value inserted ->B@634e3372,hola
Get element 2nd time-> hola
هل كانت مفيدة؟

المحلول

The description:

This class is intended primarily for use with key objects whose equals methods test for object identity using the == operator. Once such a key is discarded it can never be recreated, so it is impossible to do a lookup of that key in a WeakHashMap

means that this class is intended to use with keys, which after recreation (creating new exactly the same instance) return false when are compared to each other. In other words each new instance of such key will be unique (only return true when compared using equals() with itself and return false when compared with any other instance/object) For example String is not such kind of a class while Object is:

WeakHashMap testMapHashWeak = new WeakHashMap<Object, String>();

String value1 = "Hola1!";
String value2 = "Hola2!";

String key1 = new String("key");
String key2 = new String("key");

assert key1 != key2; // Keys are different objects...
assert key1.equals(key2); // but are equal to each other

testMapHashWeak.put(key1, value1);
testMapHashWeak.put(key2, value2);

// value2 instead of value1!
System.out.println("Get using key1 (expected Hola1): "+testMapHashWeak.get(key1));
// value2
System.out.println("Get using key2 (expected Hola2): "+testMapHashWeak.get(key2));

Object key3 = new Object();
Object key4 = new Object();

assert key3 != key4; // Keys are different objects...
assert !key3.equals(key4); // and are not equal to each other

testMapHashWeak.put(key3, value1);
testMapHashWeak.put(key4, value2);

// value2 instead of value1!
System.out.println("Get using key3 (expected Hola1):"+testMapHashWeak.get(key3)); // value1!
// value2
System.out.println("Get using key4 (expected Hola2):"+testMapHashWeak.get(key4));

WeakHashMap is an implementation of Map which has weak keys. So after key will be garbage collected the whole entry (key, value) in map will be gone. Then key word here is "after", which means not any moment sooner. So between key is marked as 'to be deleted by GC` and before it is actually deleted the map will contain key-value entry which should not be possible to obtain. In the first example with String-based keys it is possible to get the value using different key, but you cannot predict if the value will be there or not (it might have been already GCd). In the second example you can't, in any case.

نصائح أخرى

"discarded" in that sentence means that "the key object is no longer in ordinary use", no longer a live object. This is not the case for your objectB which is in use for the entire scope of test4WeakHashMap method.

From the same documentation:

An entry in a WeakHashMap will automatically be removed when its key is no longer in ordinary use. More precisely, the presence of a mapping for a given key will not prevent the key from being discarded by the garbage collector, that is, made finalizable, finalized, and then reclaimed. When a key has been discarded its entry is effectively removed from the map, so this class behaves somewhat differently from other Map implementations.

Note the "discarded by the garbage collector". Hence, "removing from a map" is not the same as "discarding".

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top