I need a Map
implementation that shares properties of both IdentityHashMap
and WeakHashMap
(reference equality instead of equals()
and weak references on keys).
What implementation do you recommend (it has to work on Android)?
The IdentityHashMap, WeakHashMap, and EnumMap all are the classes in java collection that implements the Map interface.
If you have some objects that are reused often in your application, and their construction is expensive, and there are too many of them to keep them all in memory - - you use WeakHashMap. Put there the object which is not currently used. When this object is needed - get it out of the map.
If you're suggesting Guava, then new MapMaker().weakKeys().makeMap()
would do the job directly, since weakKeys
uses reference equality for keys.
The documentation of weakKeys
says:
Specifies that each key (not value) stored in the map should be wrapped in a WeakReference (by default, strong references are used). Warning: when this method is used, the resulting map will use identity (==) comparison to determine equality of keys, which is a technical violation of the Map specification, and may not be what you expect.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With