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:
- HashMap is a part of java.util package.
- HashMap extends an abstract class AbstractMap which also provides an incomplete implementation of Map interface.
- It also implements Cloneable and Serializable interface. K and V in the above definition represent Key and Value respectively.
- HashMap doesn’t allow duplicate keys but allows duplicate values. That means A single key can’t contain more than 1 value but more than 1 key can contain a single value.
- HashMap allows null key also but only once and multiple null values.
- This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time. It is roughly similar to HashTable but is unsynchronized.
Methods in HashMap
- void clear(): Used to remove all mappings from a map.
- boolean containsKey(Object key): Used to return True if for a specified key, mapping is present in the map.
- boolean containsValue(Object value): Used to return true if one or more key is mapped to a specified value.
- Object clone(): It is used to return a shallow copy of the mentioned hash map.
- boolean isEmpty(): Used to check whether the map is empty or not. Returns true if the map is empty.
- Set entrySet(): It is used to return a set view of the hash map.
- Object get(Object key): It is used to retrieve or fetch the value mapped by a particular key.
- Set keySet(): It is used to return a set view of the keys.
- int size(): It is used to return the size of a map.
- Object put(Object key, Object value): It is used to insert a particular mapping of key-value pair into a map.
- putAll(Map M): It is used to copy all of the elements from one map into another.
- Object remove(Object key): It is used to remove the values for any particular key in the Map.
- Collection values(): It is used to return a Collection view of the values in the HashMap.
- 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).
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- replace(K key, V value): This method replaces the entry for the specified key only if it is currently mapped to some value.
- replace(K key, V oldValue, V newValue): This method replaces the entry for the specified key only if currently mapped to the specified value.
- 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.