'What‘s different between weakKeys() and weakValues() in Guava Cache?
I know weakKeys() means the keys are wrapped by WeakReference and GC may collect the keys automatically, same with weakValues()(for value).
My understanding: Once an entry (whether key or value) was collected by GC, the entry is expired.
My question:
- Are
weakKeys()andweakValues()equivalent? (one of them was collected, the entry is expired) - Guava doc said: "when this method is used, the resulting cache will use identity (==) comparison to determine equality of values". When the cache compares the value? for what?
Solution 1:[1]
Are weakKeys()andweakValues()equivalent? (one of them was collected, the entry is expired)
No, if a key or value is collected, the Entry itself will not expire. You will still have access to the Entry, but the "reference" to the key and value will point you to null.
Guava doc said: "when this method is used, the resulting cache will use identity (==) comparison to determine equality of values". When the cache compares the value? for what?
In Java, Object variables are basically pointers, which means they store an 8-bit address in the memory block containing the object. So, the documentation says that it will not compare objects (because they are null), but references to objects.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | VoidPointer |
