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

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

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. import java.util.*;
    3. public class Demo
    4. {
    5. public staticvoid main(String args[])
    6. {
    7. HashMap map =new HashMap();
    8. map.put(99,"Raj");
    9. map.put(199,"Raja");
    10. map.put(299,"Rajan");
    11. System.out.println("INITIALLY: " + map);
    12. map.clear();
    13. System.out.println("\n");
    14. System.out.println("ULTIMATELY: " + map);
    15. }
    16. }
    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. import java.util.*;
    3. public class Demo
    4. {
    5. public staticvoid main(String args[])
    6. {
    7. HashMap map1 =new HashMap();
    8. HashMap map2 =new HashMap();
    9. map1.put(99,"Raj");
    10. map1.put(199,"Raja");
    11. map1.put(299,"Rajan");
    12. map2=(HashMap)map1.clone();
    13. System.out.println("MAP ONE: " + map1);
    14. System.out.println("\n");
    15. System.out.println("MAP TWO(CLONED): " + map2);
    16. }
    17. }
    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. import java.util.*;
    3. public class Demo
    4. {
    5. public staticvoid main(String args[])
    6. {
    7. HashMap map =new HashMap();
    8. map.put(99,"Raj");
    9. map.put(199,"Raja");
    10. map.put(299,"Rajan");
    11. System.out.println("CHECK FOR KEY NINETY NINE : " + map.containsKey(99));
    12. }
    13. }
    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. import java.util.*;
    3. public class Demo
    4. {
    5. public staticvoid main(String args[])
    6. {
    7. HashMap map =new HashMap();
    8. map.put(99,"Raj");
    9. map.put(199,"Raja");
    10. map.put(299,"Rajan");
    11. System.out.println("CHECK FOR VALUE RAJAN : " +
    12. map.containsValue("Rajan"));
    13. }
    14. }
    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. import java.util.*;
    3. public class Demo
    4. {
    5. public staticvoid main(String args[])
    6. {
    7. HashMap map =new HashMap();
    8. map.put(99,"Raj");
    9. map.put(199,"Raja");
    10. map.put(299,"Rajan");
    11. String val=(String)map.get(99);
    12. System.out.println("VALUE FOR KEY NINETY NINE : " + val);
    13. }
    14. }
    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. import java.util.*;
    3. public class Demo
    4. {
    5. public staticvoid main(String args[])
    6. {
    7. HashMap map =new HashMap();
    8. map.put(7,"John");
    9. map.put(8,"Johny");
    10. map.put(9,"Jonty");
    11. boolean val=map.isEmpty();
    12. System.out.println("EMPTY: " + val);
    13. }
    14. }
    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. import java.util.*;
    3. public class Demo
    4. {
    5. public staticvoid main(String args[])
    6. {
    7. HashMap map1 =new HashMap();
    8. HashMap map2 =new HashMap();
    9. map1.put(7,"John");
    10. map1.put(8,"Johny");
    11. map1.put(9,"Jonty");
    12. System.out.println("MAP 1 : "+ map1);
    13. map2.putAll(map1);
    14. System.out.println("\n");
    15. System.out.println("MAP 2 : "+ map2);
    16. }
    17. }
    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. import java.util.*;
    3. public class Demo
    4. {
    5. public staticvoid main(String args[])
    6. {
    7. HashMap map =new HashMap();
    8. map.put(7,"John");
    9. map.put(8,"Johny");
    10. map.put(9,"Jonty");
    11. System.out.println("BEFORE: "+ map);
    12. System.out.println("\n");
    13. map.remove(8);
    14. System.out.println("AFTER: "+ map);
    15. }
    16. }
    Output
    remove( )
  • Map.values( )
    It will return all the values of the map.
    Syntax
    public Collection values()
    Example
    1. package demo;
    2. import java.util.*;
    3. public class Demo
    4. {
    5. public staticvoid main(String args[])
    6. {
    7. HashMap map =new HashMap();
    8. map.put(7,"John");
    9. map.put(8,"Johny");
    10. map.put(9,"Jonty");
    11. System.out.println("VALUES: "+ map.values());
    12. }
    13. }
    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.