HashMap Class in Java

In this article you will learn about the HashMap class in java and its various methods using NetBeans IDE.

Introduction

In Java, a map interface is an interface that consists of only unique elements. The map interface in Java consists of values that are based on key/value pairs. We can look at any value in Java using the specific key. A HashMap is a part of the map interface in Java.

HashMap Class in Java

  • HashMap consists of the values that are based on the key.
     
  • It consists of only unique elements.
     
  • The map interface is implemented and AbstractMap class is extended by HashMap.
     
  • A HashMap has no order.
     
  • It may consist of multiple null values but only one null key.

Ranking of HashMap in Java

ranking 

Example Using the NetBeans IDE

Step 1

Open the NetBeans IDE.

open NetBeans IDE 

Step 2

Create a new project.

open new project 

Step 3

Choose "Java" --> "Java Application".

choose project 

Step 4

Provide the project with a name.

project name 

Step 5

Write the following code.

  1. package demo;    
  2.     
  3. import java.util.*;    
  4.     
  5. public class Demo    
  6. {     
  7.     public static void main(String args[])    
  8.     {    
  9.         HashMap hs_mp=new HashMap();     
  10.         hs_mp.put(99,"Raj");     
  11.         hs_mp.put(199,"Raja");     
  12.         hs_mp.put(299,"Rajan");    
  13.         Set set=hs_mp.entrySet();     
  14.         Iterator itr=set.iterator();     
  15.         while(itr.hasNext())    
  16.         {     
  17.             Map.Entry mp=(Map.Entry)itr.next();     
  18.             System.out.println(mp.getKey()+" "+mp.getValue());     
  19.         }     
  20.     }     
  21. }

 

Step 6

Run the code.

(This process of working with NetBeans will be used in all the following examples.)

run file 

Output

hashmap in java 

Methods of HashMap Class in Java

  • Map.clear( )

    All the mappings from the map are removed by this method.

    Syntax
    public void clear()

    Example
    1. package demo;    
    2.     
    3. import java.util.*;    
    4.     
    5. public class Demo    
    6. {    
    7.    public staticvoid main(String args[])    
    8.    {    
    9.         HashMap map =new HashMap();    
    10.         map.put(99,"Raj");    
    11.         map.put(199,"Raja");    
    12.         map.put(299,"Rajan");    
    13.         System.out.println("INITIALLY: " + map);    
    14.         map.clear();    
    15.         System.out.println("\n");    
    16.         System.out.println("ULTIMATELY: " + map);    
    17.     }       
    18. }
    Output

    clear( )
  • Map.clone( )

    A shallow copy of the map instance is returned by this method.

    Syntax
    public Object clone()

    Example
    1. package demo;    
    2.     
    3. import java.util.*;    
    4.     
    5. public class Demo    
    6. {    
    7.    public staticvoid main(String args[])    
    8.     {    
    9.         HashMap map1 =new HashMap();    
    10.         HashMap map2 =new HashMap();    
    11.         map1.put(99,"Raj");    
    12.         map1.put(199,"Raja");    
    13.         map1.put(299,"Rajan");    
    14.         map2=(HashMap)map1.clone();    
    15.         System.out.println("MAP ONE: " + map1);    
    16.         System.out.println("\n");    
    17.         System.out.println("MAP TWO(CLONED): " + map2);      
    18.     }       
    19. }
    Output

    clone( )
  • Map.containsKey(Object key)

    In the identity HashMap, it is used to check that the given object reference is key.

    Syntax
    public boolean containsKey(Object key)
    It will return true if the given condition is satisfied.

    Example
    1. package demo;    
    2.     
    3. import java.util.*;    
    4.     
    5. public class Demo    
    6. {    
    7.    public staticvoid main(String args[])    
    8.     {    
    9.         HashMap map =new HashMap();    
    10.         map.put(99,"Raj");    
    11.         map.put(199,"Raja");    
    12.         map.put(299,"Rajan");    
    13.         System.out.println("CHECK FOR KEY NINETY NINE :  " + map.containsKey(99));    
    14.     }       
    15. }
    Output

    containsKey(Object key)
  • Map.containsValue(Object value)

    The identity HashMap is used to check that the given object is a value.

    Syntax
    public boolean containsValue(Object value)

    Will return true if the map consists of one or more keys.

    Example
    1. package demo;    
    2.     
    3. import java.util.*;    
    4.     
    5. public class Demo    
    6. {    
    7.    public staticvoid main(String args[])    
    8.     {    
    9.         HashMap map =new HashMap();    
    10.         map.put(99,"Raj");    
    11.         map.put(199,"Raja");    
    12.         map.put(299,"Rajan");    
    13.         System.out.println("CHECK FOR VALUE RAJAN : " +    
    14.         map.containsValue("Rajan"));    
    15.    }       
    16. }
    Output

    containsValue(Object value)
  • Map.get(Object key)

    A value is returned to which the given key is mapped. A null is returned if there is no mapping.

    Syntax
    public A get(Object key)

    Example
    1. package demo;    
    2.     
    3. import java.util.*;    
    4.     
    5. public class Demo    
    6. {    
    7.    public staticvoid main(String args[])    
    8.     {    
    9.         HashMap map =new HashMap();    
    10.         map.put(99,"Raj");    
    11.         map.put(199,"Raja");    
    12.         map.put(299,"Rajan");    
    13.         String val=(String)map.get(99);    
    14.         System.out.println("VALUE FOR KEY NINETY NINE : " + val);    
    15.    }       
    16. }
    Output

    get(Object key)
     

  • Map.isEmpty( )

    It is checked by this method that the map consists of no key-value mappings.

    Syntax
    public boolean isEmpty()
    It will return true if the given condition is satisfied.

    Example

    1. package demo;    
    2.     
    3. import java.util.*;    
    4.     
    5. public class Demo    
    6. {    
    7.    public staticvoid main(String args[])    
    8.     {    
    9.         HashMap map =new HashMap();    
    10.         map.put(7,"John");    
    11.         map.put(8,"Johny");    
    12.         map.put(9,"Jonty");    
    13.         boolean val=map.isEmpty();    
    14.         System.out.println("EMPTY: " + val);    
    15.     }       
    16. }  

    Output

    isEmpty( )
     

  • Map.putAll( )

    It is used to copy mappings from one map to the other.

    Syntax
    public void putAll(Map m)

    Example

    1. package demo;    
    2.     
    3. import java.util.*;    
    4.     
    5. public class Demo    
    6. {    
    7.    public staticvoid main(String args[])    
    8.     {    
    9.         HashMap map1 =new HashMap();    
    10.         HashMap map2 =new HashMap();    
    11.         map1.put(7,"John");    
    12.         map1.put(8,"Johny");    
    13.         map1.put(9,"Jonty");    
    14.         System.out.println("MAP 1 : "+ map1);    
    15.         map2.putAll(map1);    
    16.         System.out.println("\n");    
    17.         System.out.println("MAP 2 : "+ map2);    
    18.    }       
    19. }
    Output

    putAll( )

  • Map.remove( )

    Mapping is removed for the specific key in the given map.

    Syntax
    public A remove(Object key)

    Example 

    1. package demo;    
    2.     
    3. import java.util.*;    
    4.     
    5. public class Demo    
    6. {    
    7.    public staticvoid main(String args[])    
    8.     {    
    9.         HashMap map =new HashMap();         
    10.         map.put(7,"John");    
    11.         map.put(8,"Johny");    
    12.         map.put(9,"Jonty");    
    13.         System.out.println("BEFORE: "+ map);    
    14.         System.out.println("\n");    
    15.         map.remove(8);    
    16.         System.out.println("AFTER: "+ map);    
    17.     }       
    18. }
    Output

    remove( )
     

  • Map.values( )

    It will return all the values of the map.

    Syntax
    public Collection values()

    Example

    1. package demo;    
    2.     
    3. import java.util.*;    
    4.     
    5. public class Demo    
    6. {    
    7.    public staticvoid main(String args[])    
    8.     {    
    9.         HashMap map =new HashMap();         
    10.         map.put(7,"John");    
    11.         map.put(8,"Johny");    
    12.         map.put(9,"Jonty");    
    13.         System.out.println("VALUES: "+ map.values());    
    14.    }       
    15. }
    Output 

    values( ) 

Constructors Supported by HasMap

  • HashMap( )
    A default HashMap is created by this constructor.
     
  • HashMap(m)
    HashMap is initialized by this constructor.
     
  • HashMap( int capacity)
    Capacity is initialized by this constructor.
     
  • HashMap(int capacity, float fillRatio)
    Capacity and the fill ratio are initialized by this constructor.

Summary

This article explains the HashMap class and its various methods in Java.