HashMap Methods Tutorials in Java With Examples

HashMap is a one-to-one relationship between one object and other object. Let us take a real life example of hash map for better understanding of this concept:

Let’s assume you want to open your email account. But to do that you’re taken to a login screen where you type in your username and password. When the combination is correct only then your email account open.

A hashmap works in a similar way. You have to give it an appropriate input so you can get the proper output. Each input is mapped to exactly one output. In our previous example, one combination of username and password produces exactly one email account.

HashMap is a class that implements Map Interface and Extends AbstractMap class.  It is generally denoted as HashMap <key, value> or HashMap <K,V>.


HashMap Methods in JAVA:

Let us discuss all the HashMap methods one by one with Examples in Java.

1. Object put(Object key, Object value):

This method adds the key value pair to the HashMap object, as shown in the following program:

import java.util.*;

public class HashMapDemoClass 
{

  public static void main(String args[]) {

// create a hashmap

  HashMap hashmapobj = new HashMap();

// using Object put(Object key, Object value) method

  hashmapobj.put(1001, "I");

  hashmapobj.put(1002, "Love");

  hashmapobj.put(1003, "Java");

//printing the hashmap

  System.out.println(" All the key value pairs " + hashmapobj);

 }

}

Output:

All the key value pairs {1001=I, 1002=Love, 1003=Java}

2. int size():

This method returns the size of the HashMap as shown in the following program:

package com.test;
import java.util.*;

public class HashMapDemoClass 
{

  public static void main(String args[]) {

 // create a hashmap

  HashMap hashmapobj = new HashMap();

  hashmapobj.put(1001, "I");

  hashmapobj.put(1002, "Love");

  hashmapobj.put(1003, "Java");

  //using size() method

  System.out.println(" Size of hashmap is " + hashmapobj.size());

 }

}

Output:

Size of hashmap is 3

3. void clear():

This method clears all the key value pairs in the HashMap, as shown in the following program:

import java.util.*;

public class HashMapDemoClass 
{

   public static void main(String args[]) {

  // create a hashmap

   HashMap hashmapobj = new HashMap();

   hashmapobj.put(1001, "I");

   hashmapobj.put(1002, "Love");

   hashmapobj.put(1003, "Java");

  //using size() method

   System.out.println(" Size of hashmap is " + hashmapobj.size());

   //using clear() method

   hashmapobj.clear();

  //using size() method  after clearing the hashmap

   System.out.println(" Size of hashmap after clearing is " + hashmapobj.size());

 }

}

Output:

Size of hashmap is 3

Size of hashmap after clearing is 0

4. Object clone():

This method returns the exact copy of the HashMap, as shown in the following program:

package com.test;
import java.util.*;

public class HashMapDemoClass 
{

  public static void main(String args[]) {

 // create a hashmap

  HashMap hashmapobj1 = new HashMap();

  hashmapobj1.put(1001, "I");

  hashmapobj1.put(1002, "Love");

  hashmapobj1.put(1003, "Java");

 //Creating a new hashmap

  HashMap hashmapobj2 = new HashMap();

 //Using clone method

  hashmapobj2=(HashMap)hashmapobj1.clone();

  System.out.println("Cloned 2nd Map: " + hashmapobj2);

 }

}

Output:

Cloned 2nd Map: {1001=I, 1002=Love, 1003=Java}

5. boolean containsKey(Object key):

This method checks the presence of specified  key in the HashMap, it will return true if the key is present and false if the mentioned key is not present, as shown in the following program:

package com.test;

import java.util.*;

public class HashMapDemoClass 
{

  public static void main(String args[]) {

  // create a hashmap

  HashMap hashmapobj1 = new HashMap();

  hashmapobj1.put(1001, "I");

  hashmapobj1.put(1002, "Love");

  hashmapobj1.put(1003, "Java");

  // check existence of key 1002

  System.out.println("Check if key 1002 exists: " + hashmapobj1.containsKey(1002));

  // check existence of key 1000

  System.out.println("Check if key 1000 exists: " + hashmapobj1.containsKey(1000));

 }

}

Output:

Check if key 1002 exists: true

Check if key 1000 exists: false

6. boolean containsValue(Object value):

This method checks the presence of specified value in the HashMap, it will return true if the value is present and false if the mentioned value is not present, as shown in the following program:

import java.util.*;

public class HashMapDemoClass 
{

  public static void main(String args[]) {

 // create a hashmap

  HashMap hashmapobj1 = new HashMap();

  hashmapobj1.put(1001, "I");

  hashmapobj1.put(1002, "Love");

  hashmapobj1.put(1003, "Java");

 // check existence of value 'I'

  System.out.println("Check if value 'I' exists: " +  hashmapobj1.containsValue("I"));

// check existence of value 'India'

  System.out.println("Check if value 'India' exists: " +  hashmapobj1.containsValue("India"));

 }

}

Output:

Check if value 'I' exists: true

Check if value 'India' exists: false

7. Set entrySet():

This method returns a Set view of the mappings contained in the HashMap, as shown in the following program:

package com.test;

import java.util.*;

public class HashMapDemoClass 
{

  public static void main(String args[]) {

  // create a hashmap

  HashMap hashmapobj1 = new HashMap();

  hashmapobj1.put(1001, "I");

  hashmapobj1.put(1002, "Love");

  hashmapobj1.put(1003, "Java");

  // create set of hashmap

  Set set=hashmapobj1.entrySet();

  System.out.println("Set values: " + set);

 }

}

Output:

Set values: [1001=I, 1002=Love, 1003=Java]

8. Object get(Object key):

This method returns the value corresponding to the specified key, as shown in the following program:

import java.util.*;

public class HashMapDemoClass 
{

  public static void main(String args[]) {

  // create a hashmap

  HashMap hashmapobj1 = new HashMap();

  hashmapobj1.put(1001, "I");

  hashmapobj1.put(1002, "Love");

  hashmapobj1.put(1003, "Java");

  // get value of key 1003

  String str=(String)hashmapobj1.get(1003);

  System.out.println("Value for key 1003 is: " + str);

 }

}

Output:

Value for key 1003 is: Java

9.boolean isEmpty():

This method as the name suggests checks whether the HashMap is empty or not, It will return true if it is empty, or false if it is not empty, as shown in the following program:

import java.util.*;

public class HashMapDemoClass 
{

  public static void main(String args[]) {

  // create a hashmap

  HashMap hashmapobj1 = new HashMap();

  hashmapobj1.put(1001, "I");

  hashmapobj1.put(1002, "Love");

  hashmapobj1.put(1003, "Java");

  // check if the map is empty or not

  System.out.println(" Hashmap is empty??" + hashmapobj1.isEmpty());

 }

}

Output:

Hashmap is empty??false

10. Set keySet():

This method returns a Set view of the keys contained in the HashMap ,as shown in the following program:

import java.util.*;

public class HashMapDemoClass 
{

  public static void main(String args[]) {

  // create a hashmap

  HashMap hashmapobj1 = new HashMap();

  hashmapobj1.put(1001, "I");

  hashmapobj1.put(1002, "Love");

  hashmapobj1.put(1003, "Java");

  // seting the keyset

  Set keyset=hashmapobj1.keySet();

  // check key set values

  System.out.println("Key values are: " + keyset);

 }

}

Output:

Key values are: [1001, 1002, 1003]

11. putAll(Map m):

This method copies all the key value pair form one hashmap to another , as shown in the following program:

import java.util.*;

public class HashMapDemoClass 
{

  public static void main(String args[]) {

  // create a hashmap

  HashMap hashmapobj1 = new HashMap();

  hashmapobj1.put(1001, "I");

  hashmapobj1.put(1002, "Love");

  hashmapobj1.put(1003, "Java");

  System.out.println("Values in newmap1: "+ hashmapobj1);

  HashMap hashmapobj2 = new HashMap();

  // put all values in newmap2

  hashmapobj2.putAll(hashmapobj1);

  System.out.println("Values in hashmapobj2: "+ hashmapobj2);

  }

}

Output:

Values in newmap1: {1001=I, 1002=Love, 1003=Java}

Values in hashmapobj2: {1001=I, 1002=Love, 1003=Java}

12. Object remove(Object key):

This method as the name specifies removes the key value pair, corresponding to the mentioned key in the argument list, as shown in the following program:

import java.util.*;

public class HashMapDemoClass 
{

  public static void main(String args[]) {

  // create a hashmap

  HashMap hashmapobj1 = new HashMap();

  hashmapobj1.put(1001, "I");

  hashmapobj1.put(1002, "Love");

  hashmapobj1.put(1003, "Java");
  System.out.println("hashmap before  using remove method: "+ hashmapobj1);

  // remove value for key 1002

  hashmapobj1.remove(1002);

  System.out.println("hashmap after  using remove method: "+ hashmapobj1);

  }

}

Output:

hashmap before  using remove method: {1001=I, 1002=Love, 1003=Java}

hashmap after  using remove method: {1001=I, 1003=Java}

13. Collection values():

This method returns the collection of all the values in the hashmap, as shown in the following program:

import java.util.*;

public class HashMapDemoClass 
{

  public static void main(String args[]) {

  // create a hashmap

  HashMap hashmapobj1 = new HashMap();

  hashmapobj1.put(1001, "I");

  hashmapobj1.put(1002, "Love");

  hashmapobj1.put(1003, "Java");

  System.out.println("collection view of this hashmap is "+ hashmapobj1.values());

  }

}

Output:

collection view of this hashmap is [I, Love, Java]