How to implement a Map with multiple keys? [duplicate] How to implement a Map with multiple keys? [duplicate] java java

How to implement a Map with multiple keys? [duplicate]


Two maps. One Map<K1, V> and one Map<K2, V>. If you must have a single interface, write a wrapper class that implements said methods.


Commons-collections provides just what you are looking for: https://commons.apache.org/proper/commons-collections/apidocs/

Looks like now the commons-collections is typed.

A typed version can be found at: https://github.com/megamattron/collections-generic

This will exactly support your use case:

 MultiKeyMap<k1,k2,...,kn,v> multiMap = ??


I'm still going suggest the 2 map solution, but with a tweest

Map<K2, K1> m2;Map<K1, V>  m1;

This scheme lets you have an arbitrary number of key "aliases".

It also lets you update the value through any key without the maps getting out of sync.