Introduction

HashMap in Java is a Map based collection class that is used for storing Key & value pairs, it is denoted as HashMap<K, V>. It is similar to the Hashtable class except that it is unsynchronized and permits nulls (null values and null key). It is not an ordered collection which means it does not return the keys and values in the same order in which they have been inserted into the HashMap.

Create a HashMap

To use HashMap class import java.util.HashMap package. Following constructor can be used to create HashMap

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

// Constructs an empty HashMap with the specified initial capacity and default load factor (0.75)
HashMap(int initialCapacity)

// Constructs an empty HashMap with the specified initial capacity and load factor
HashMap(int initialCapacity, float loadFactor)

// Constructs a new HashMap with the same mappings as the specified Map
HashMap(Map<? extends K,? extends V> m)

initialCapacity specifies how many entries can be stored in HashMap. Default loadFactor of this hashmap is 0.75. When HashMap is filled by 75%, the entries are moved to a new HashMap of double the size of the original HashMap.

Example

public class HashMapExample { 
  public static void main(String args[]) { 
    /* This is how to declare HashMap */ 
    HashMap<Integer, String> hmap = new HashMap<Integer, String>(); 
    
    /*Adding elements to HashMap*/ 
    hmap.put(12, "Chaitanya"); 
    hmap.put(2, "Rahul"); 
    
    /* Display content using Iterator*/ 
    Set set = hmap.entrySet(); 
    Iterator iterator = set.iterator(); 
    
    while(iterator.hasNext()) { 
      Map.Entry mentry = (Map.Entry)iterator.next(); 
      System.out.print("key is: "+ mentry.getKey() + " & Value is: "); 
      System.out.println(mentry.getValue()); 
    } 
    
    /* Get values based on key*/ 
    String var= hmap.get(2); 
    System.out.println("Value at index 2 is: "+var); 
    
    /* Checking Key Existence */ 
    boolean flag = hashmap.containsKey(22); 
    
    /* Checking Value Existence */ 
    boolean flag = hashmap.containsValue("Singh"); 

    /* Fetching all keys */ 
    Set<Integer> keySet = hashmap.keySet(); 

    /* Remove values based on key*/ 
    hmap.remove(3);
  } 
}

Important functions

  • void clear()
    Removes all of the mappings from this map.
  • boolean containsKey(Object key)
    Returns true if this map contains a mapping for the specified key.
  • boolean containsValue(Object value)
    Returns true if this map maps one or more keys to the specified value.
  • V get(Object key)
    Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
  • boolean isEmpty()
    Returns true if this map contains no key-value mappings.
  • V put(K key, V value)
    Associates the specified value with the specified key in this map.
  • V remove(Object key)
    Removes the mapping for the specified key from this map if present.
  • boolean remove(Object key, Object value)
    Removes the entry for the specified key only if it is currently mapped to the specified value.
  • V replace(K key, V value)
    Replaces the entry for the specified key only if it is currently mapped to some value.
  • int size()
    Returns the number of key-value mappings in this map.
  • Collection<V> values()
    Returns a Collection view of the values contained in this map.
  • Set<Map.Entry<K,V>> entrySet()
    Returns a Set view of the mappings contained in this map.
  • Set<K> keySet()
    Returns a Set view of the keys contained in this map.
  • V 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.

Reference

Class HashMap