I'm looking for an equivalent to the
WeakHashMap<K, V>
class, except that it maps multiple keys to a value, so it's really more like
WeakHashMap<K1, K2, V>
WeakHashMap<K1, K2, K3, V>
etc.
The way you get
and set
entries would be like a multi-column primary key in a database: you put items using multiple keys e.g. (K1, K2)
, and to get that item back out you need to provide all the same keys you used to put it in. Given these get
& set
semantics, the GC semantics would be: an entry would be GCed when no longer reachable, which means that any of its keys is no longer reachable.
Has anyone else needed something like this before? How would you approach such a requirement? Storing a Tuple as the key, like you could do in a non-weak HashMap, doesn't work (the Tuple gets GCed almost immediately, with nobody pointing to it).
If something like this has been made before I'd be happy to use it, but just trying to think of how I would construct such a thing out of WeakReferences and a normal hashmap and am coming up with a blank.