Alternative to associative PHP arrays in JAVA

In PHP, I could use an array with strings as keys. for example $ some_array ["cat"] = 123; $ some_array ["dog"] = 456; I just switched to Java and I cannot find a data structure that can do this. Is it possible?

+4
source share
5 answers

What you are describing is an associative array also called a table, dictionary, or map.

In Java, you need the Map interface and possibly the HashMap class as an implementation.

 Map<String, Integer> myMap = new HashMap<String, Integer>(); myMap.put("cat", 123); Integer value = myMap.get("cat"); //123 
+7
source

You would use one of Map , for example HashMap , to do this.

+2
source

You want to use Map , most likely HashMap .

+1
source

The data structure you are in is Map . I consider it an abstract class, so you have to use one of its specific subclasses, for example HashMap<?>

+1
source

Like many people, a map is what you are looking for. If this was your choice, be sure to override hashcode () and equals (). Take a look because it is necessary: http://www.ibm.com/developerworks/java/library/j-jtp05273/index.html

0
source

All Articles