What I need is a collection which allows multiple keys to access a single object.
I need to apply frequent alterations to this object.
It also must be efficient for 500k+ entries.
Java has several implementations of the interface Map, each one with its own particularities. However, none of the existing Java core Map implementations allow a Map to handle multiple values for a single key.
In this way, we can insert multiple values associated with the same key into the HashMap using Collections.
Class MultiKeyMap<K,V> A Map implementation that uses multiple keys to map the value. This class is the most efficient way to uses multiple keys to map to a value.
Example 3: Update value of Hashmap using merge() In the above example, the merge() method adds the old value and new value of the key First . And, insert the updated value to HashMap . To learn more, visit HashMap merge().
I sort of interpreted his request differently. What if one wants two completely different keysets to access the same underlying values. For example:
"Hello" ------|
|----> firstObject
3 ------|
"Monkey" ------|
|----> secondObject
72 ------|
14 -----------> thirdObject
"Baseball" ------|
|----> fourthObject
18 ------|
Obviously having two maps, one for the integer keys and one for the String keys, isn't going to work, since an update in one map won't reflect in the other map. Supposing you modified the Map<String,Object>
, updating "Monkey" to map to fifthObject. The result of this modification is to change the Entry<String,Object>
within that map, but this of course has no effect on the other map. So whilst what you intended was:
"Monkey" ------|
|----> fifthObject
72 ------|
what you'd get in reality would be this:
"Monkey" -----------> fifthObject
72 -----------> secondObject
what I do in this situation is to have the two side by side maps, but instead of making them say Map<String, Integer>
I would make them Map<String, Integer[]>
, where the associated array is a single member array. The first time I associate a key with a value, if no array exists yet and the key returns null, I create the array, and associate any other key I wish to with it (in that key's map). Subsequently, I only modify the array's contents, but never the reference to the array itself, and this works a charm.
"Monkey" -------> fifthObjectArray ------|
|-----> fifthObjectArray[0]
72 -------> fifthObjectArray ------|
Any implementation of java.util.Map<K,V>
will do this - there is no restriction on how many times a particular value can be added under separate keys:
Map<String,Integer> m = new HashMap<String, Integer>();
m.put("Hello", 5);
m.put("World", 5);
System.out.println(m); // { Hello->5, World->5 }
If you want a map where a single key is associated with multiple values, this is called a multi-map and you can get one from the google java collections API or from Apache's commons-collections
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With