In PHP I could use an array with strings as keys. eg $some_array["cat"] = 123; $some_array["dog"] = 456; I just switched to Java and I can't find a data structure capable of doing this. Is this possible?
What you are describing is an associative array, also called a table, dictionary, or map.
In Java, you want the Map
interface, and probably the HashMap
class as the implementation.
Map<String, Integer> myMap = new HashMap<String, Integer>();
myMap.put("cat", 123);
Integer value = myMap.get("cat"); //123
You would use one of the Map implementations such as HashMap to do that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With