Hash Map
Java Implementation of Map
Methods
containsKey(K key): Returns true if this map contains a mapping for the specified key.
containsValue(V value): Returns true if this map maps one or more keys to the specified value.
put(K key, V value): Associates the specified value with the specified key in this map; If the map previously contained a mapping for the key, the old value is replaced by the specified value
remove(Object key): Removes the mapping for a key from this map if it is present
get(Object key): Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
Collection<> values(): Returns a Collection view of the values contained in this map.
for (Integer value : map.values()) {
System.out.println("Value = " + value);
}
- Set<> getKeys(): Returns a Set view of the keys contained in this map.
for (Integer key : map.keySet()) {
System.out.println("Key = " + key);
}
- Traverse Through Map:
// Version 1: Use entry set to traverse
for (Map.Entry<K, V> entry : map) {
entry.getKey(); // get key object
entry.getValue(); // get value object
}