Skip to content

Java HashMap Tutorial with Examples

Java HashMap tutorial with examples will help you understand how to use Java HashMap in an easy way. HashMap in Java is a hashtable implementation of the Map interface which provides all the optional Map operations. The HashMap class in Java allows null as a key or a value. However, since the keys cannot be duplicated, there can only be one null key in the HashMap object.

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

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

Java HashMap Tutorial

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

The HashMap in Java is not a synchronized implementation, it means if multiple threads are simultaneously modifying the HashMap object, the access must be synchronized externally. The synchronizedMap method of Collections class should be used to get the synchronized Map from the HashMap object as given below.

You can also use the existing hashmap object instead of creating a new one to get the synchronized map.

How to create objects of the HashMap class?

The HashMap class in Java provides several constructors to create objects. The below given default constructor creates a new and empty HashMap object.

The keys and values of this hashmap object will be of type String.

The below given overloaded HashMap constructor will create a new hashmap object from the existing map object like TreeMap or another HashMap.

The hashmap object created by this constructor will have the same mappings as the specified map.

How to copy TreeMap to HashMap using the HashMap constructor?

The below given example shows how to copy a TreeMap object to HashMap using this constructor.

Output

How to copy HashMap to another HashMap using the HashMap constructor?

Similar to the example given above, instead of TreeMap, you can also copy a HashMap object to another HashMap object using this constructor.

Output

HashMap Methods

The below given are some of the important methods of the HashMap class in Java.

How to add key value mappings to HashMap using the put, putAll and putIfAbsent methods?

The put method of the HashMap class adds a key value mapping to the hashmap object.

If the key does not exist in the hashmap, a new mapping will be added and null is returned. If the key exists in the HashMap, the old value mapped to the given key will be replaced by the new value. In this case, an old value will be returned.

Output

The put method replaces a value for a key if the key already exists in the map. If you want to add mapping if and only if the key does not exist in the map, use the putIfAbsent method.

The putIfAbsent method adds a key-value mapping to the HashMap if the key does not already exist in the map and returns null. If the key already exists in the map, the value associated with the key is returned.

Output

Difference between put and putIfAbsent methods: If the key does not exist in the map, both the methods behave the same. If the key exists in the map, the put method replaces an old value with the new value, while the putIfAbsent method does not.

How to add all mappings of another Map (HashMap, TreeMap) to HashMap?

The putAll method of the HashMap class adds all mappings of the specified Map object to this HashMap object.

If the hashmap object already contains the key, the value will be replaced with the value of the specified map for the given key.

The below given example shows how to copy all mapping from the TreeMap to HashMap (or copy all mappings from the HashMap to another HashMap object).

Output

How to get a value for a given key from the HashMap object using the get and getOrDefault methods?

The get method of the HashMap class returns a value associated with the specified key. If the key does not exist in the HashMap, it returns null.

Output

As you can see from the output, the returned null value does not necessarily mean that the HashMap does not have the specified key. It may be possible that the given key is associated with a null value in the map.

The getOrDefault method returns a specified default value if the key does not exist in the map instead of returning null.

Output

Difference between get and getOrDefault methods: If the specified key exists in the map, both the methods return a value associated with the specified key. But if the key does not exist in the map, the get method returns null, while the getOrDefault method returns the specified default value.

How to get the size of the HashMap (HashMap length) using the size method?

The HashMap size method returns the number of key value mappings contained in the HashMap object.

Output

The size method returns 0 if the HashMap is empty.

How to check if HashMap is empty using the isEmpty method?

The HashMap isEmpty method returns true if this map object does not contain any key value mappings. It returns false if this map has at least one mapping.

Output

How to get all the keys of HashMap using the keySet method?

The keySet method of HashMap class returns a set view of all the keys contained in the HashMap object.

Output

The set returned by the keySet method is backed by the original HashMap object. So any changes you make to the set will also be reflected in the HashMap object, and vice versa.

Output

How to get all values of HashMap using the values method?

The HashMap values method returns Collection view of the values contained in the map object.

Output

How to get all mappings (entries) of HashMap using the entrySet method?

The entrySet method of the Java HashMap class returns a set view of entries i.e. key-value pairs contained in this hashmap object. The entry set returned by this method is backed by the original map object, so if you make any changes to the entry set it will reflect in the map, and vice versa.

Output

How to remove a key value mapping from the HashMap using the remove method?

The HashMap remove method removes a key-value mapping from the map for the specified key.

The remove method returns the value associated with the key or null if the key does not exist in the map. A null can also be returned if the specified key was mapped to a null value.

Output

What if you want to remove a key-value pair, if and only if the specified key is associated with a specific value? Well, there is an overloaded version of the remove method that accepts key and value arguments.

This method removes the mapping if and only if the given key is associated with the given value as given below.

Output

How to remove all the mappings from the HashMap (empty HashMap) using the clear method?

The clear method of the HashMap class removes all mappings from the HashMap object (i.e. empty hashmap or clear hashmap). The HashMap object becomes empty after this method call.

Output

How to replace a value for the given key of HashMap using the replace method?

The HashMap replace method replaces a value mapped to a given key only if it mapped to some value.

The old value associated with the specified key will be replaced by the specified new value. This method returns the previously associated value with the key if the key exists. It returns null if the key does not exist in the hashmap or the key was previously associated with the null value.

Output

The above given code replaces a value for the given key if the key is mapped to any value. What if you want to replace the value for a given key if and only if the key is mapped to a specific value? In that case, you need to use an overloaded replace method.

This method replaces the value of a given key if and only if the key is mapped to a given value. This method returns true if the key exists and the old value is replaced with a new value. It returns false if the key does not exist in the map or the key is not mapped to the specified value.

Output

How to check if HashMap contains a key using the containsKey method?

The containsKey method returns true if this HashMap object contains a mapping for the specified key. It returns false otherwise.

Output

Note: If the HashMap key is an object of a custom class, the class must override the equals and hashCode methods for the containsKey method to work.

How to check if HashMap contains a value using the containsValue method?

The cotnainsValue of the HashMap class returns true if the specified value is mapped to one or more keys in the map. It returns false otherwise.

Output

Note: Just like the containsKey method, If the specified value is an object of a custom class, the class must override the equals and hashCode methods for the containsValue method to work.

How to Iterate HashMap in Java?

There are several ways to iterate over HashMap in Java.

1. Iterate through keys of HashMap

You can iterate over hashmap keys using the keySet method and enhanced for loop as given below.

Output

2. Iterate through values of HashMap

You can iterate over hashmap values using the values method and enhanced for loop.

Output

3. Iterate through all mappings of HashMap (key-value pairs or entries)

You can get all the hashmap entries using the entrySet method and then iterate over the entries using the enhanced for loop as given below.

Output

Please refer to the complete how to iterate Java HashMap example to know more.

How to clone HashMap in Java using the clone method?

The clone method of the HashMap class returns a shallow copy of this map object. The keys and values themselves are not copied, only the references are copied. Refer to below given HashMap clone example to understand it.

Output

The below given HashMap examples will help you HashMap concepts in more detail.

Java HashMap Examples

References:
Java HashMap Javadoc

Please let me know if you liked the Java HashMap tutorial with examples in the comments section below.

About the author

Leave a Reply

Your email address will not be published.