Store collection of key-value pairs.
We import the hashmap and declare it by specifying both the key and the value data type.
!! cannot use primitive
import java.util.HashMap;
HashMap<String, Integer> myHashMap = new HashMap<String, Integer>();
myHashMap.put("foo", 1);
Use .get() with the key.
myHashMap.get("foo");
Use .remove() with the key;
myHashMap.remove("foo");
myHashMap.clear();
myHashMap.size();
Use a for-each loops
for(String key : myHashMap.keySet()) {
// code
}
myHashMap.containsKey(key);
myHashMap.replace(key, value);
Create a Set with all the keys in the hashmap.
myHashMap.keySet();
Return a collection with all the values (without their key).
myHashMap.values();