Java WeakHashMap with multiple keys?

I'm looking for an equivalent

WeakHashMap<K, V> 

except that it maps multiple keys to a value, so it looks more like

 WeakHashMap<K1, K2, V> WeakHashMap<K1, K2, K3, V> etc. 

The write way get and set will look like a primary key with several columns in the database: you put elements using several keys, for example. (K1, K2) , and to return this element, you need to provide all the same keys that you used to enter it. Given these semantics of get and set , the semantics of GC will be: the record will be GCed if there is no longer access, which means that any of its keys is no longer available.

Has anyone else needed something similar before? How do you approach this requirement? Storing a tuple as a key, as you could do in a weak HashMap, does not work (Tuple receives GCed almost immediately, and no one points to it).

If something like this was done before I was happy to use it, but just try to think about how I would build such a thing from WeakReferences and a regular hash map, and I would come up with a space.

+6
source share
1 answer

An interesting problem. I do not know any implementations of this, but I would approach it by adapting the source for WeakHashMap . It uses a ReferenceQueue and checks it at the beginning of almost any public method, deleting entries for each gc'ed referent.

Here is my rough essay on how to adapt WeakHashMap to a weak map with multiple keys:

  • Define the composite key that you described and use it for the master record set
  • Maintain an internal map from a key component to a set of keys in which it participates
  • When you find the reference component in the ReferenceQueue , find the key set in which it participates and remove these keys from the main record set
  • You also need to remove each of the keys in which it participates from each other set of key components. That is, suppose you find the key component c 1 in the reference queue and participates in the keys k 1 , ..., k n , then for each k i , if the other key components are c 2 and c 3 , you need to remove k i from the key sets for c 2 and c 3 , and also delete the entire record for c 1 .
  • When the key set for the component is empty, you should probably also delete the key set map entry for this component. This means that you can find a component in the link queue that does not have the corresponding data in a map with a set of components to the key.

The fly in Mazen here is that all of these internal cards must be set up so as not to interfere with the key component being gc'ed. That is, in this structure with several keys there should not be hard (or soft) links. WeakHashMap accomplishes this by creating its inner class Entry (which implements Map.Entry ) extends WeakReference . When the key is gc'ed, it is an Entry object, not the key itself, which is placed in the link queue. Something similar should have been used in the design of all internal structures (a composite key object, a set of records, a map from a key component to a set of keys, and a set of keys themselves).

+1
source

All Articles