dsa

Java HashMap

In this tutorial, we will learn about the Java HashMap class and its methods with the help of examples.

The HashMap class of the Java collections framework provides the hash table implementation of the Map interface.

HashMap is a part of Java’s collection since Java 1.2. It provides the basic implementation of the Map interface of Java. It stores the data in (Key, Value) pairs. To access a value one must know its key. This class uses a technique called Hashing. Hashing is a technique of converting a large String to small String that represents the same String. A shorter value helps in indexing and faster searches. Let’s see how to create a map object using this class.

Example

// Java Program to illustrate the Hashmap Class 
import java.util.*; 

public class Codemistic { 

	public static void main(String[] args) 
	{ 
		Map<String, Integer> map 
			= new HashMap< >(); 

		map.put("ayush", 10); 
		map.put("anvi", 30); 
		map.put("pragya", 20); 

		for (Map.Entry<String, Integer> e : map.entrySet()) 
			System.out.println(e.getKey() + " "
							+ e.getValue()); 
	} 
} 
output:
ayush 20
anvi 10
pragya 30

Important Features of HashMap

Few important features of HashMap are:

Methods in HashMap

  1. void clear(): Used to remove all mappings from a map.
  2. boolean containsKey(Object key): Used to return True if for a specified key, mapping is present in the map.
  3. boolean containsValue(Object value): Used to return true if one or more key is mapped to a specified value.
  4. Object clone(): It is used to return a shallow copy of the mentioned hash map.
  5. boolean isEmpty(): Used to check whether the map is empty or not. Returns true if the map is empty.
  6. Set entrySet(): It is used to return a set view of the hash map.
  7. Object get(Object key): It is used to retrieve or fetch the value mapped by a particular key.
  8. Set keySet(): It is used to return a set view of the keys.
  9. int size(): It is used to return the size of a map.
  10. Object put(Object key, Object value): It is used to insert a particular mapping of key-value pair into a map.
  11. putAll(Map M): It is used to copy all of the elements from one map into another.
  12. Object remove(Object key): It is used to remove the values for any particular key in the Map.
  13. Collection values(): It is used to return a Collection view of the values in the HashMap.
  14. compute(K key, BiFunction<K, V> remappingFunction):This method Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping).
  15. computeIfAbsent(K key, Function<K> mappingFunction): This method If the specified key is not already associated with a value (or is mapped to null), attempts to compute its value using the given mapping function and enters it into this map unless null.
  16. computeIfPresent(K key, BiFunction<K, V> remappingFunction):This method 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.
  17. forEach(BiConsumer<K, V> action): This method Performs the given action for each entry in this map until all entries have been processed or the action throws an exception.
  18. getOrDefault(Object key, V defaultValue): This method returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.
  19. merge(K key, V value, BiFunction<K, V> remappingFunction): This method If the specified key is not already associated with a value or is associated with null, associates it with the given non-null value.
  20. putIfAbsent(K key, V value): This method 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.
  21. replace(K key, V value): This method replaces the entry for the specified key only if it is currently mapped to some value.
  22. replace(K key, V oldValue, V newValue): This method replaces the entry for the specified key only if currently mapped to the specified value.
  23. replaceAll(BiFunction<K, V> function): This method replaces each entry’s value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception.