Guava MapMaker (). WeakKeys (). MakeMap () vs WeakHashMap

We have a Scala server that receives a node tree using protocol buffers via a socket, and we need to attach additional data to each node.

In one streaming context, and when the node tree and the data associated with it are deleted at the same time (due to exit from the scope), is there any reason to use Google Guava MapMaker with weak keys () through the use of WeakHashMap? It seems that with MapMaker it pays for synchronized access, which in this case is not needed.

As an aside, it would be useful if MapMaker had to provide access to equivalence settings so that you could choose link equality, but not care about weak or soft links.

+6
java guava java.util.concurrent weakhashmap
source share
1 answer

One significant drawback of WeakHashMap is that it is not an "identity card". That is, it uses equals() and hashCode (and not == and identityHashCode ) on keys, which really makes no sense for weak keys. You can get around this error by making sure your keys use identity equality in the equals method.

+6
source share

All Articles