Java – HashMap Class

  •  HashMap contains values based on the key. It implements the Map interface and extends AbstractMap class.
  • HashMap  is used for storing Key & value pairs.
  • HashMap contains only unique elements.
  • HashMap may have one null key and multiple null values.
  • HashMap doesn’t maintains insertion order.
  • HashMap is similar to the Hashtable class except that it is unsynchronized and permits null values and null key.
  • HashMap maintains key and value pairs and often denoted as HashMap<Key, Value> .

 

Constructors of HashMap class as shown below :

  • HashMap (): It is a default constructors hashmap.
  • HashMap (Map m): It initializes the hash map by using the elements of m.
  • HashMap (int capacity): It initializes the capacity of the hash map to capacity. The default initial capacity of HashMap will be 16 and the load factor will be 0.75.Load factor represents at what level HashMap should be increase your capacity .
  • HashMap (int capacity, float fillRatio): it initializes capacity and fillRatio to grow the capacity after certain elements inserted.

HashMap Example in Java:

  1. import java.util.*;
  2. class HapMapDemo{
  3.  public static void main(String args[]){
  4.   HashMap<Integer,String> hm=new HashMap<Integer,String>();
  5.   hm.put(100,“A”);
  6.   hm.put(101,“B”);
  7.   hm.put(102,“C”);
  8.   for(Map.Entry mentry : hm.entrySet()){
  9.    System.out.println(mentry .getKey()+” “+mentry .getValue());
  10.   }
  11.  }
  12. }
Output:
       102 C
       100 A
       101 B

HashMap Class Methods

  1. void clear(): It removes all the key and value pairs from the specified Map.
  2. Object clone(): It returns a copy of all the mappings of a map and used for cloning them into another map.
  3. boolean containsKey(Object key): It is a boolean function which returns true or false based on whether the specified key is found in the map.
  4. boolean containsValue(Object Value): Similar to containsKey() method, however it looks for the specified value instead of key.
  5. Value get(Object key): It returns the value for the specified key.
  6. boolean isEmpty(): It checks whether the map is empty. If there are no key-value mapping present in the map then this function returns true else false.
  7. Set keySet(): It returns the Set of the keys fetched from the map.
  8. value put(Key k, Value v): Inserts key value mapping into the map. Used in the above example.
  9. int size(): Returns the size of the map – Number of key-value mappings.
  10. Collection values(): It returns a collection of values of map.
  11. Value remove(Object key): It removes the key-value pair for the specified key. Used in the above example.
  12. void putAll(Map m): Copies all the elements of a map to the another specified map.

 

Another HashMap Example in Java:

import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
import java.util.Set;
public class HashMapDemo1 {

   public static void main(String args[]) {

      // Declare HashMap 
      HashMap<Integer, String> hmap = new HashMap<Integer, String>();

      // Adding elements to HashMap
      hmap.put(11, "A");
      hmap.put(20, "B");
      hmap.put(17, "C");
      hmap.put(10, "D");
      hmap.put(31, "E");

      // Display element using Iterator 
      Set set = hmap.entrySet();
      Iterator iterator = set.iterator();
      while(iterator.hasNext()) {
         Map.Entry mhm = (Map.Entry)iterator.next();
         System.out.print("key = "+ mhm.getKey() + " & Value = "+mhm.getValue());
        
      }

      // Get values based on key
      String var= hmap.get(20);
      System.out.println("Value at index 2 is: "+var);

      // Remove values based on key
      hmap.remove(31);
      System.out.println("Map key and values after removal:");
      Set set2 = hmap.entrySet();
      Iterator iterator2 = set2.iterator();
      while(iterator2.hasNext()) {
          Map.Entry mentry2 = (Map.Entry)iterator2.next();
          System.out.print("Key = "+mentry2.getKey() + " & Value = "+mentry2.getValue());
        
       }

   }
}

Output:

key = 10 & Value = D
key = 20 & Value = B
key = 31 & Value = E
key = 17 & Value = C
key = 11 & Value = A
Value at index 2 is: B
Map key and values after removal:
Key = 10 & Value = D
Key = 20 & Value = B
Key = 17 & Value = C
Key = 11 & Value = A

2 thoughts on “Java – HashMap Class

Leave a comment