Java HashMap is a hash table implementation of the Map interface. The HashMap provides all the optional Map operations. It also allows null keys and values.

The HashMap class is contained in the java.util package. You have to import the java.util package using import statement to use the HashMap class in the code.

The HasMap class implements the Map interface and extends AbstractMap class. The HashMap does not guarantee the order of its elements. That means that the element may not be returned in the same order in which they were inserted in the map.

The HashMap stores keys and values as objects, we cannot store primitive values in the HashMap. If required, they need to be converted to respective wrapper classes before putting them into the HashMap.

The HashMap is not a synchronized implementation, that means if multiple threads are simultaneously accessing and modifying the HashMap, it must be synchronized externally. The synchronizedMap method of Collections class cab be used to get the synchronized Map from the HashMap object.

Below given are some of the Java HashMap examples which shows how to use HashMap in Java.

Java HashMap Examples

Join 1000+ fellow learners! Enter your email address below: