Combination of IdentityHashMap and WeakHashMap

I need a Map implementation that shares the properties of both IdentityHashMap and WeakHashMap (instead of referential equality instead of equals() and weak key references).

What implementation do you recommend (it should work on Android)?

+8
java android referenceequals map weak-references
source share
1 answer

If you offer Guava, then new MapMaker().weakKeys().makeMap() will do the job directly, since weakKeys uses referential equality for keys.

The weakKeys documentation says:

Indicates that each key (not value) stored on the map should be wrapped in WeakReference (strong links are used by default). Warning: when this method is used, the resulting map will use an identity comparison (==) to determine key equality, which is a technical violation of the map specification and may not be what you expect.

+2
source share

All Articles