views:

2031

answers:

4

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.

+9  A: 

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

oxbow_lakes
+2  A: 

Uhm…

Map map = new HashMap();
Object someValue = new Object();
map.put(new Object(), someValue);
map.put(new Object(), someValue);

Now the map contains the same value twice, accessible via different keys. If that’s not what you’re looking for you should rework your question. :)

Bombe
A: 

this may do what you want:

import java.util.*;
class Value {
    public String toString() {
     return x.toString();
    }
    Integer x=0;
}
public class Main {
    public static void main(String[] arguments) {
     Map m=new HashMap();
     final Value v=new Value();
     m.put(1,v);
     m.put(2,v);
     System.out.println(m.get(1));
     System.out.println(m.get(2));
     v.x=42;
     System.out.println(m.get(1));
     System.out.println(m.get(2));
    }
Ray Tayek
A: 

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 ------|
fragorl
And it doesn't have to be two different classes that comprise the keysets either - they could both be strings, for example
fragorl