This is probably one of the most useful references I use, that I can never remember. I bookmark my references and keep going back. I'm putting it here just in case the other one disappears.
An example of a java HashMap is as follows:
Map<Integer, String> myMap = new HashMap<>();
It can contain only objects (no primitives), and you can iterate through the keys, the values, or both.Iterating through the keys:
for(Integer key : myMap.keySet()) {...
}
Iterating through the values:
for(String value : myMap.values()) {...}Iterating through both the keys and the values:for(Map.Entry<Integer, String> entry : myMap.entrySet()) {Integer keyVal = entry.getKey();String val = entry.getValue();...}
Comments
Post a Comment