Map is an object that maps keys to values. A map cannot contain duplicate keys.
HashMap class is a hash table based implementation of the Map interface. It makes no guarantees concerning the order of iteration; in particular, it does not guarantee that the order will remain constant over time.
HashMap class permits null values and the null key.
Java provides the HashMap class as part of the Java Collections Framework. For an overview of the Java Collections Framework, check out my post Overview of the Java Collections Framework.
The following example illustrates the HashMap class.
Example
Output
As you can see from the above output, HashMap didn’t preserve the insertion order of the elements.
The above program showed the use of some of the common methods of the HashMap class. For a complete list of all the available methods, refer the official docs.
Now since you know how to use the HashMap class, check out other implementations of the Map interface: