Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a HashMap as a Key for another HashMap

I have a requirement where I need to map a set of configurations with a set of values, ideally denoted by a Map<Map<String, Object>, Map<String, Object>> structure.

Both the configurations & the values part of the main Map are arbitrary & hence, I am unable to use a concrete class.

Please provide some feedback on this structure. Can a Map be used as a key for another Map. Doing a bit of research, I was able to establish that the Map's equals method utilizes all underlying Keys & Values to deem two Maps as equal. Also, the HashCode of a Map is based on the Hashcodes of the Map's Keys. This IMO should suffice the minimum requirements of using a Map as a key.

I would still like someone to validate this before I go ahead with the implementation. In case there is a better solution / design that someone can suggest, please feel free to do so.

EDIT

I ended up using a simple tilde ('~') & pipe ('|') separated String as the key & deconstructed it whenever needed. Thanks to all who helped.

like image 568
Sumit Avatar asked Feb 17 '17 09:02

Sumit


People also ask

Can I use map as a key for map?

You can't use a Map as a key but you can use it as a value. To explain further, since you are likely to be adding to the HashMap the key will not remain constant and thus will no longer serve it's purpose as a key. You can have a Map as key but as you pointed out since a Map is (typically) mutable the key changes.

Can I put a HashMap in a HashMap?

Method 2: Using putAll(k, v) Method. putAll(k,v) method is used to copy one HashMap to another empty HashMap.

Can HashMap key be paired?

Use a Pair as keys for the HashMap . JDK has no Pair, but you can either use a 3rd party libraray such as http://commons.apache.org/lang or write a Pair taype of your own.

Does HashMap support duplicate keys?

HashMap stores key, value pairs and it does not allow duplicate keys. If the key is duplicate then the old key is replaced with the new value.


1 Answers

Yes, a HashMap can be used as a key to another map, as the class properly overrides .equals() and .hashCode().

However it's broadly speaking a bad idea to use mutable types (such as HashMap) as Map keys or Set elements, because you violate the invariants these classes expect if the objects are mutated while in the collection.

While not quite what you're looking for, Guava offers several additional data structures such as Multiset, MultiMap, BiMap, Table which may be useful. They also offer immutable collections such as ImmutableMap which (because they can't be mutated) are safer to use as a Map key. Which isn't to say you should do so, simply that it's safe (if the keys and values are also immutable).

Consider posting a question exploring the problem that lead you to conclude a Map<Map<K, V>, Map<K, V>> structure was what you needed. You may get better answers to that question.

like image 97
dimo414 Avatar answered Sep 26 '22 05:09

dimo414