Map – HashMap stores/retrieves objects by key
The Map interface itself is not related to the Collection interface directly, but it uses the Set interface for its keys and Collection for its values. For example, for Map<Integer, String> map:
Set<Integer> keys = map.keySet();Collection<String> values = map.values();
Each value is stored in a map with a unique key that is passed in along with the value when added to the map. In the case of Map<Integer, String> map:
map.put(42, "whatever"); //42 is the key for the value "whatever"
Then later, the value can be retrieved by its key:
String v = map.get(42); System.out.println(v); //prints: whatever
These are the basic map operations that convey the Map interface's purpose – to provide a storage for key-value pairs, where both—key and value—are objects and the class used as the key implements the equals() and hashCode() methods, which override the default implementations in the Object class.
Now, let's take a closer...