Combo of IdentityHashMap and WeakHashMap

2.4k Views Asked by At

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)?

1

There are 1 best solutions below

3
Louis Wasserman On

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.