views:

246

answers:

4

I want a data structure that allows me to map keys to values, like PHP's associative arrays. Each key can only exist once, but a value can be mapped to any number of keys. What am I looking for? Something in the Google Commons Collections?

+3  A: 

You're looking for a HashMap, such as a HashMap<K,V>. The former maps Objects to Objects, and the latter maps types of your choosing (like other generics).

Andrew Coleson
PHP arrays maintain insertion order. HashMap does not.
cletus
Good point, but he only asked for the associative property, so I assume he's not concerned about that quirk.
Andrew Coleson
@cletus: Then there's `LinkedHashMap`. :-P
Chris Jester-Young
And if you want keys to be sorted the you can use TreeMap
DroidIn.net
+5  A: 

The Map structure is what you want. A good implementation is the HashMap.

This data type does not allow the same value for the Key, but you can have as many duplicate values as you like.

Example usage:

Map<String, String> map = new HashMap<String, String>();
map.put("FirstName", "LastName");

System.out.println(map.get("FirstName")); // Prints 'LastName'
System.out.println(map.put("FirstName", "Foo")); // Prints 'LastName'
System.out.println(map.get("FirstName")); // Prints 'Foo'

In other words, the key can only exist once. Otherwise the value is overwritten.

jjnguy
I think he wants multiple keys to point to the same object though I may be wrong
DroidIn.net
No, `Map<K,V>` is what he wants. A key can exist only once, but a value can be mapped to any number of keys. So in the case of `( "Foo" => foo, "Bar" => bar, "Baz" = foo)`, the keys "Foo" and "Baz" are unique, but they map to the same value, foo.
William Brendel
Aaa - isn't that the same thing? Multiple keys - same value, that's what you get with HashMap if you stick the same reference multiple times.
DroidIn.net
HashMaps are awesome.
monksy
A: 

What's wrong with regular HashMap? If your values are Strings you'll get reference counting for free and if you need to refer to a particular instance of object just don't allocate one, but use existing reference

DroidIn.net
Reference counting? Can you further explain that point or how it affects in a managed language?
David Rodríguez - dribeas
-1: the sentence about reference counts is totally incorrect. No decent JVM will use reference counting for memory management of Strings or any other "normal" data structures.
Stephen C
`String a = "A";String b = "A";if (a == b else System.out.println(0);1`So - I suppose the actual mechanism that compiler deploys is not brute force RC but it's not what I implied, all I am saying is that in HashMap your multiple String keys will be mapped to the same instance of String value and you don't have to do anything special about that since in Java Strings are treated in the special way
DroidIn.net
A: 

HashMap is the way to go for sure. I actually view things the other way around when I first started coding in PHP. I was wondering, how do I implement a HashMap? Then I discovered associative arrays.

test