In this article we will dive into Java HashMap and it’s most commonly used methods. Java HashMap is one of the most commonly used implementation of Map interface.

  1. Characteristics of Java HashMap

  1. HasMap class implements the java.util.Map interface, the under lying data structure is hash table.
  2. Duplicate keys are not allowed but values can be duplicated.
  3. Insertion order is not preserved and it is based on hash code of the keys.
  4. Java HashMap class is non synchronized.
  5. The initial default capacity of Java HashMap class is 16 with a load factor of 0.75. Which means once the map reaches its capacity, the capacity will be increased by 3/4 of initial capacity.

Basic usage of HashMap in java is to store key value pairs. Each key value pair we call an Entry. Map is a collection of Entry objects.

2. Hierarchy of java HashMap class

Below image illustrates the hierarchy of Java HashMap class.

Java HashMap

3. Creating HashMap instance in java

Copy to Clipboard

Constructs an empty HashMap with the default initial capacity (16) and the default load factor (0.75).

4. Java HashMap methods

As stated at the beginning, map is a collection of entries and each entry is a key value pair. Let’s have a look into most commonly used HashMap methods that how we process map with simple examples.

4.1. put(), putAll(), putIfAbsent()

  1. put(k, v)  – To associate the specified value with the specified key in this map.
  2. putAll(Map<? extends K,? extends V> m  – Copies all of the mappings from the specified map to this map.
  3. putIfAbsent(K key, V value) -(Since Java 8)  – If the specified key is not already associated with a value (or is mapped to null) associates it with the given value and returns null, else returns the current value.
Copy to Clipboard

4.2. containsKey(), containsValue()

  1. containsKey(Object key)   – Returns true if this map contains a mapping for the specified key.
  2. containsValue(Object value)  – Returns true if this map maps one or more keys to the specified value.
Copy to Clipboard

4.3. get(), getOrDefault()

  1. get(Object key)  – Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
  2. getOrDefault(Object key, V defaultValue)  – Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.
Copy to Clipboard

4.4. isEmpty(), size()

  1. isEmpty()  -Returns true if this map contains no key-value mappings.
  2. size() – Returns the number of key-value mappings in this map.
Copy to Clipboard

4.5. replace(), remove(), clear()

  1. remove(Object key)  – Removes the mapping for the specified key from this map if present.
  2. remove(Object key, Object value)  – Removes the entry for the specified key only if it is currently mapped to the specified value.
  3. replace(K key, V value)  – Replaces the entry for the specified key only if it is currently mapped to some value.
  4. replace(K key, V oldValue, V newValue) – Replaces the entry for the specified key only if currently mapped to the specified value.
  5. clear()  – Removes all of the mappings from this map.
Copy to Clipboard

Conclusion:

In this article we saw how to use how to create HashMap in Java and its most commonly used methods with simple demonstration examples.

Still there are few important methods to discuss like how Java HashMap iterating, merging and compute , they will be discussed each one in separate article.

Reference