Java HashMap is one of the most popular Collection classes in java. Java HashMap is Hash table based implementation. HashMap in java extends AbstractMap class that implements Map interface.

Java HashMap

java-hashmap

Some of the important points about HashMap in Java are;

  1. Java HashMap allows null key and null values.
  2. HashMap is not an ordered collection. You can iterate over HashMap entries through keys set but they are not guaranteed to be in the order of their addition to the HashMap.
  3. HashMap is almost similar to Hashtable except that it’s unsynchronized and allows null key and values.
  4. HashMap uses it’s inner class Node<K,V> for storing map entries.
  5. HashMap stores entries into multiple singly linked lists, called buckets or bins. Default number of bins is 16 and it’s always power of 2.
  6. HashMap uses hashCode() and equals() methods on keys for get and put operations. So HashMap key object should provide good implementation of these methods. This is the reason immutable classes are better suitable for keys, for example String and Interger.
  7. Java HashMap is not thread safe, for multithreaded environment you should use ConcurrentHashMap class or get synchronized map using Collections.synchronizedMap() method.

Java HashMap Constructors

Java HashMap provides four constructors.

  1. public HashMap(): Most commonly used HashMap constructor. This constructor will create an empty HashMap with default initial capacity 16 and load factor 0.75
  2. public HashMap(int initialCapacity): This HashMap constructor is used to specify the initial capacity and 0.75 load factor. This is useful in avoiding rehashing if you know the number of mappings to be stored in the HashMap.
  3. public HashMap(int initialCapacity, float loadFactor): This HashMap constructor will create an empty HashMap with specified initial capacity and load factor. You can use this if you know the maximum number of mappings to be stored in HashMap. In common scenarios you should avoid this because load factor 0.75 offers a good tradeoff between space and time cost.
  4. public HashMap(Map<? extends K, ? extends V> m): Creates a Map having same mappings as the specified map and with load factor 0.75

Java HashMap Constructors Example

Below code snippet is showing HashMap example of using all the above constructors.

Java HashMap Methods

Let’s have a look at the important methods of HashMap in java.

  1. public void clear(): This HashMap method will remove all the mappings and HashMap will become empty.
  2. public boolean containsKey(Object key): This method returns ‘true’ if the key exists otherwise it will return ‘false’.
  3. public boolean containsValue(Object value): This HashMap method returns true if the value exists otherwise false.
  4. public Set<Map.Entry<K,V>> entrySet(): This method returns a Set view of the HashMap mappings. This set is backed by the map, so changes to the map are reflected in the set, and vice-versa.
  5. public V get(Object key): Returns the value mapped to the specified key, or null if there is no mapping for the key.
  6. public boolean isEmpty(): A utility method returning true if no key-value mappings are present.
  7. public Set<K> keySet(): Returns a Set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa.
  8. public V put(K key, V value): Associates the specified value with the specified key in this map. If the map previously contained a mapping for the key, the old value is replaced.
  9. public void putAll(Map<? extends K, ? extends V> m): Copies all of the mappings from the specified map to this map. These mappings will replace any mappings that this map had for any of the keys currently in the specified map.
  10. public V remove(Object key): Removes the mapping for the specified key from this map if present.
  11. public int size(): Returns the number of key-value mappings in this map.
  12. public Collection<V> values(): Returns a Collection view of the values contained in this map. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa.

There are many new methods in HashMap introduced in Java 8.

  1. public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction): If the specified key is not already associated with a value (or is mapped to null), this method attempts to compute its value using the given mapping function and enters it into the HashMap unless Null.
  2. public V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction): If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value.
  3. public V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction): This HashMap method attempts to compute a mapping for the specified key and its current mapped value.
  4. public void forEach(BiConsumer<? super K, ? super V> action): This method performs the given action for each entry in this map.
  5. public V getOrDefault(Object key, V defaultValue): Same as get except that defaultValue is returned if no mapping found for the specified key.
  6. public V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction): If the specified key is not already associated with a value or is associated with null, associates it with the given non-null value. Otherwise, replaces the associated value with the results of the given remapping function, or removes if the result is null.
  7. public V putIfAbsent(K key, V value): 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.
  8. public boolean remove(Object key, Object value): Removes the entry for the specified key only if it is currently mapped to the specified value.
  9. public boolean replace(K key, V oldValue, V newValue): Replaces the entry for the specified key only if currently mapped to the specified value.
  10. public V replace(K key, V value): Replaces the entry for the specified key only if it is currently mapped to some value.
  11. public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function): Replaces each entry’s value with the result of invoking the given function on that entry.

Java HashMap Example

Here is a simple java program for HashMap commonly used methods.

Below is the output of above Java HashMap example program.

How HashMap works in java?

HashMap in java use it’s inner class Node<K,V> for storing mappings. HashMap works on hashing algorithm and uses hashCode() and equals() method on key for get and put operations.

HashMap use singly linked list to store elements, these are called bins or buckets. When we call put method, hashCode of key is used to determine the bucket that will be used to store the mapping.

Once bucket is identified, hashCode is used to check if there is already a key with same hashCode or not. If there is an existing key with same hashCode, then equals() method is used on key. If equals returns true, then value is overwritten, otherwise a new mapping is made to this singly linked list bucket. If there is no key with same hashCode then mapping is inserted into the bucket.

For HashMap get operation, again key hashCode is used to determine the bucket to look for the value. After bucket is identified, entries are traversed to find out the Entry using hashCode and equals method. If match is found, value is returned otherwise null is returned.

There are much more things involved such as hashing algorithm to get the bucket for the key, rehashing of mappings etc. But for our working, just remember that HashMap operations work on Key and good implementation of hashCode and equals method is required to avoid unwanted behaviour. Below image shows the explanation of get and put operations.

java-hashmap-entry-impl (1)

Recommended Read: hashCode and equals method importance in Java

Java HashMap Load Factor

Load Factor is used to figure out when HashMap will be rehashed and bucket size will be increased. Default value of bucket or capacity is 16 and load factor is 0.75. Threshold for rehashing is calculated by multiplying capacity and load factor. So default threshold value will be 12. So when the HashMap will have more than 12 mappings, it will be rehashed and number of bins will be increased to next of power 2 i.e 32. Note that HashMap capacity is always power of 2.

Default load factor of 0.75 provides good tradeoff between space and time complexity. But you can set it to different values based on your requirement. If you want to save space, then you can increase it’s value to 0.80 or 0.90 but then get/put operations will take more time.

Java HashMap keySet

Java HashMap keySet method returns the Set view of keys in the HashMap. This Set view is backed by HashMap and any changes in HashMap is reflected in Set and vice versa. Below is a simple program demonstrating HashMap keySet examples and what is the way to go if you want a keySet not backed by map.

Output of the above program will make it clear that keySet is backed by map.

Java HashMap values

Java HashMap values method returns a Collection view of the values in the Map. This collection is backed by HashMap, so any changes in HashMap will reflect in values collection and vice versa. A simple example below confirms this behaviour of HashMap values collection.

Output of above program is below.

Java HashMap entrySet

Java HashMap entrySet method returns the Set view of mappings. This entrySet is backed by HashMap, so any changes in map reflects in entry set and vice versa. Have a look at the below example program for HashMap entrySet example.

Below is the output produced by above program.

Java HashMap putIfAbsent

A simple example for HashMap putIfAbsent method introduced in Java 8.

Output of above program is;

Java HashMap forEach

HashMap forEach method is introduced in Java 8. It’s a very useful method to perform the given action for each entry in the map until all entries have been processed or the action throws an exception.

Output of above HashMap forEach example program is;

Java HashMap replaceAll

HashMap replaceAll method can be used to replace each entry’s value with the result of invoking the given function on that entry. This method is added in Java 8 and we can use lambda expressions for this method argument.

Output of above HashMap replaceAll program is;

Java HashMap computeIfAbsent

HashMap computeIfAbsent method computes the value only if key is not present in the map. After computing the value, it’s put in the map if it’s not null.

Output of above program is;

Java HashMap computeIfPresent

Java HashMap computeIfPresent method recomputes the value if the specified key is present and value is not-null. If the function returns null, the mapping is removed.

Output produced by HashMap computeIfPresent example is;

Java HashMap compute

If you want to apply a function on all the mappings based on it’s key and value, then compute method should be used. If there is no mapping and this method is used, value will be null for compute function.

Output of HashMap compute example is;

Java HashMap merge

If the specified key is not present or is associated with null, then associates it with the given non-null value. Otherwise, replaces the associated value with the results of the given remapping function, or removes if the result is null.

Output of above program is;

That’s all for HashMap in Java, I hope that nothing important is missed. Share it with others too if you liked it.

Reference: API Doc

By admin

Leave a Reply

%d bloggers like this: