3

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?

Nick Heiner
  • 119,074
  • 188
  • 476
  • 699
  • This question discusses something similar. http://stackoverflow.com/questions/4993366/does-java-support-associative-arrays/12640402#12640402 – lahiru madhumal Sep 28 '12 at 12:54

4 Answers4

7

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
  • 136,852
  • 53
  • 295
  • 323
  • I think he wants multiple keys to point to the same object though I may be wrong – Bostone Oct 08 '09 at 03:19
  • 2
    No, `Map` 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 Oct 08 '09 at 03:29
  • 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. – Bostone Oct 08 '09 at 04:27
  • In PHP associative arrays, the association can be heterogeneous. For example: PHP supports: array(key1 => string1, key2 => array(..), key3 => Object) Not sure if declaring hashmap as new HashMap replicates this behaviour. – Yo Yo Money Singh Mar 30 '16 at 11:07
4

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
  • 10,100
  • 8
  • 32
  • 30
0

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

Bostone
  • 36,858
  • 39
  • 167
  • 227
  • Reference counting? Can you further explain that point or how it affects in a managed language? – David Rodríguez - dribeas Oct 08 '09 at 06:02
  • 1
    -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 Oct 08 '09 at 06:50
  • `String a = "A"; String b = "A"; if (a == b && a.equals(b)) System.out.println(1); 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 – Bostone Oct 08 '09 at 16:28
0

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.