Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need a Java map/table with multiple keys to one value. Value is commonly altered

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.

like image 271
Duncan Avatar asked Aug 06 '09 08:08

Duncan


People also ask

Can a map have multiple values for same key Java?

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.

Can a HashMap have multiple values for same key?

In this way, we can insert multiple values associated with the same key into the HashMap using Collections.

Can a map have multiple keys?

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.

How do I change the key-value of a map in Java?

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().


2 Answers

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 ------|
like image 83
fragorl Avatar answered Nov 09 '22 23:11

fragorl


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

like image 20
oxbow_lakes Avatar answered Nov 09 '22 23:11

oxbow_lakes