GroTechMinds

HashMap and its methods

HashMap and its methods

Before going into HashMap let us discuss the following concepts

1. What is Map?

2. Map Hierarchy

What is Map?

Map is an interface in Java which stores multiple datas in the form of key and value pairs. Map interfaces come from java.utils package. Map accept unique keys and it does not accept duplicate keys. In case duplicate keys are given then only one among the duplicate  Key’ s value having highest length will be displayed. We can access individually all th keys and their respective values present in the Map.

Map Hierarchy

The below diagram shows the Map hierarchy consisting of classes and interfaces.

Map Table

Now let us discuss about HashMap  and its methods

What is HashMap?

HashMap is the implemented class of Map interface used for storing elements in the form of key and value pairs. It is present in java.utils package. It allows to store only one null key and any number of null values. It allows storage of duplicate values. It allows faster storage and retrieval of elements.In HashMap elements are not added in an orderly manner. It is neither thread safe nor synchronized. It increases its capacity when its load factor becomes 0.75. Its threshold is 0.75 times current capacity.

Methods of HashMap

Below are some of the methods of HashMap:

Method Name Description
put(Object key, Object value) It is used to add a pair of key and value into the HashMap.
putAll(Map m) It is used to add all pairs of keys and values from one HashMap to another HashMap.
clear() It is used to clear all key and value pairs present in HashMap.
size() It is used to display the size of the key and value pairs present in HashMap.
equals(Object o) It is used to check if key and value pairs present in one HashMap is equal to the key and value pairs present in another HashMap. It gives ‘true’ value if key and value pairs present in two HashMaps are equal and ‘false’ value if key and value pairs present in two HashMaps are not equal.
containsKey(Object key) It is used to check if the specified key is present among all the key and value pairs of HashMap class. It gives ‘true’ value if the specified Key is present among all the key and value pairs and ‘false’ value if the specified key is not present among all the key and value pairs.
containsValue(Object value) It is used to check if the specified value of a key is present among all the key and value pairs of HashMap class. It gives ‘true’ value if the specified value of a key is present among all the key and value pairs and ‘false’ value if the specified value of a key is not present among all the key and value pairs.
get(Object key) It is used to display the value of a specified key among all key and value pairs of HashSet. It displays ‘null’ if the specified key is not present among all key and value pairs of HashMap.
isEmpty() It is used to check if the HashMap class is empty or not. It display ‘true’ value if the HashMap class is empty and ‘false’ value if the HashMap class is not empty.
keySet() It is used to display the keys among all the key and value pairs present in HashMap.
remove(Object key) It is used to remove a specified key and value pair from the HashMap. If we try to print a key which is not present in HashMap it will display null value but if we try to print a key which is present in HashMap it will display its corresponding value.
values() It is used to display the corresponding values of the keys present in the HashMap
putIfAbsent(Object key, Object value) Using this method, the specified key and value pairs get added alongwith the key and value pairs present in HashMap if that key and value pair is absent in the HashMap. If the specified key and value is present in the HashMap the specified key and value pair will not get added again in the HashMap. This method will also display the value of the specified key and value pair if that key and value pair is present in HashMap. If the specified key and value pair is not displayed in HashMap then null will be displayed.
replace(Object key, Object oldValue, Object newValue) It is used to replace the old value of the specified key with a new value. Here we also mention the old value of the specified key.
replace(Object key, Object value) It is used to replace the old value of the specified key with a new value.
entrySet() It is used to display all the key and value pairs present in HashMap.
iterator() It is used to iterate sequentially all the key and value pairs present in HashMap.
clone() It is used to create a shallow copy of the key and value pairs present in HashMap.

1. Let us check how  put(Object key, Object value) method works in HashMap class.

 Code Snippet:

				
					package hashMapclass;
      import java.util.HashMap;
      public class HashMapClass {
	 public static void main(String[] args) {
		HashMap m1= new HashMap();
      m1.put("Rajaraman","PerformanceTesting" );
      m1.put("Manish", "FunctionalTesting");
      m1.put("Ekta", "Mentor1");
      m1.put("Eeshan", "Mentor2");
      m1.put("Neeki","ManualTesting");
      System.out.println(m1);
 	}
   }

                
           Output: {Eeshan=Mentor2, Ekta=Mentor1, Neeki=ManualTesting, Rajaraman=PerformanceTesting, Manish=FunctionalTesting}
				
			

Screenshot:

h1

2. Let us check how  putAll(Map m) method works in HashMap class.

 

Code Snippet:

				
					package hashMapclass;
     import java.util.HashMap;
     public class HashMapClass {
	public static void main(String[] args) {
		HashMap m1= new HashMap();
    m1.put("Rajaraman","PerformanceTesting" );
    m1.put("Manish", "FunctionalTesting");
    m1.put("Ekta", "Mentor1");
m1.put("Eeshan", "Mentor2");
m1.put("Neeki","ManualTesting");
  System.out.println(m1);
HashMap m2= new HashMap();
m2.putAll(m1);
System.out.println(m2);


	}
}

Output:  {Eeshan=Mentor2, Ekta=Mentor1, Neeki=Instructor3, Rajaraman=Instructor2, Manish=Instructor1}
{Eeshan=Mentor2, Rajaraman=Instructor2, Ekta=Mentor1, Manish=Instructor1, Neeki=Instructor3}

				
			

Screenshot:

h2

3. Let us check how  clear() method works in HashMap class.

Code Snippet:

				
					package hashMapclass;
import java.util.HashMap;
public class HashMapClass {
	public static void main(String[] args) {
		HashMap m1= new HashMap();
m1.put("Rajaraman","PerformanceTesting" );
m1.put("Manish", "FunctionalTesting");
m1.put("Ekta", "Mentor1");
m1.put("Eeshan", "Mentor2");
m1.put("Neeki","ManualTesting");
m1.clear();
System.out.println(m1);
	}
}


       Output: {}

				
			

 Screenshot:

h3

4. Let us check how  size() method works in HashMap class.

Code Snippet:

				
					package hashMapclass;
import java.util.HashMap;
public class HashMapClass {
	public static void main(String[] args) {
		HashMap m1= new HashMap();
		m1.put("Rajaraman","PerformanceTesting" );
		m1.put("Manish", "FunctionalTesting");
		m1.put("Ekta", "Mentor1");
		m1.put("Eeshan", "Mentor2");
		m1.put("Neeki","ManualTesting");
		System.out.println(m1.size());
	}
}

Output: 5

				
			

Screenshot:

h4

5. Let us check how  equals(Object o) method works in HashMap class.

Code Snippet:

				
					package hashMapclass;
import java.util.HashMap;
public class HashMapClass {
	public static void main(String[] args) {
		HashMap m1= new HashMap();
		m1.put("Rajaraman","PerformanceTesting" );
		m1.put("Manish", "FunctionalTesting");
		m1.put("Ekta", "Mentor1");
		m1.put("Eeshan", "Mentor2");
		m1.put("Neeki","ManualTesting");
		HashMap m2= new HashMap();
		m2.put("Rajaraman","PerformanceTesting" );
		m2.put("Manish", "FunctionalTesting");
		m2.put("Ekta", "Mentor1");
		m2.put("Eeshan", "Mentor2");
		m2.put("Neeki","ManualTesting");
		HashMap m3= new HashMap();
		m3.put("Rajaraman","PerformanceTesting" );
		m3.put("Manish", "FunctionalTesting");
		m3.put("Ekta", "Mentor1");
		m3.put("Eeshan", "Mentor2");
		m3.put("Neeki","ManualTesting");
		m3.put("Deepesh", "Instructor4");
		System.out.println("Is HashMap m1 equals m2 ?: "+m1.equals(m2));
		System.out.println("Is HashMap m1 equals m3 ?: "+m1.equals(m3));
	}
}

Output: Is HashMap m1 equals m2 ?: true
Is HashMap m1 equals m3 ?: false

				
			

Screenshot:

h5

6. Let us check how  containsKey(Object key) method works in HashMap class.

Code Snippet:  

				
					package hashMapclass;
import java.util.HashMap;
public class HashMapClass {
	public static void main(String[] args) {
		HashMap m1= new HashMap();
		m1.put("Rajaraman","PerformanceTesting" );
		m1.put("Manish", "FunctionalTesting");
		m1.put("Ekta", "Mentor1");
		m1.put("Eeshan", "Mentor2");
		m1.put("Neeki","ManualTesting");
		System.out.println("Does the HashMap contains Rajaraman key?: "+m1.containsKey("Rajaraman"));
	 System.out.println("Does the HasMap contains Deepesh key?: "+m1.containsKey("Deepesh"));
	}
}

          Output: Does the HashMap contains Rajaraman key?: true
Does the HasMap contains Deepesh key?: false

				
			

Screenshot:

h6

7. Let us check how  containsValue(Object value) method works in HashMap class.

Code Snippet:  

				
					package hashMapclass;
import java.util.HashMap;
public class HashMapClass {
	public static void main(String[] args) {
		HashMap m1= new HashMap();
		m1.put("Rajaraman","PerformanceTesting" );
		m1.put("Manish", "FunctionalTesting");
		m1.put("Ekta", "Mentor1");
		m1.put("Eeshan", "Mentor2");
		m1.put("Neeki","ManualTesting");
System.out.println("Does the HashMap contains PerformanceTesting value?: "+m1.containsValue("PerformanceTesting"));
System.out.println("Does the HasMap contains Python value?: "+m1.containsValue("Python"));
	}
}

Output:  Does the HashMap contains PerformanceTesting value?: true
    Does the HasMap contains Python value?: false

				
			

Screenshot:  

h7

8. Let us check how  get(Object key) method works in HashMap class.

Code Snippet:  

				
					package hashMapclass;
import java.util.HashMap;
public class HashMapClass {
	public static void main(String[] args) {
		HashMap m1= new HashMap();
		m1.put("Rajaraman","PerformanceTesting" );
		m1.put("Manish", "FunctionalTesting");
		m1.put("Ekta", "Mentor1");
		m1.put("Eeshan", "Mentor2");
		m1.put("Neeki","ManualTesting");
System.out.println(m1.get("Manish"));
System.out.println(m1.get("Deepesh"));
	}
}

Output:
FunctionalTesting
null

				
			

Screenshot:

h8

9. Let us check how  isEmpty() method works in HashMap class.

Code Snippet:

				
					package hashMapclass;
import java.util.HashMap;
public class HashMapClass {
	public static void main(String[] args) {
		HashMap m1= new HashMap();
		m1.put("Rajaraman","PerformanceTesting" );
		m1.put("Manish", "FunctionalTesting");
		m1.put("Ekta", "Mentor1");
		m1.put("Eeshan", "Mentor2");
		m1.put("Neeki","ManualTesting");
System.out.println("Is HashMap m1 empty? :" +m1.isEmpty());
HashMap m2= new HashMap();
System.out.println("Is HashMap m2 empty? :" +m2.isEmpty());
	}
}
Output: Is HashMap m1 empty? :false
Is HashMap m2 empty? :true

				
			

Screenshot:

h9

10. Let us check how  keySet() method works in HashMap class.

Code Snippet:

				
					package hashMapclass;
import java.util.HashMap;
public class HashMapClass {
	public static void main(String[] args) {
		HashMap m1= new HashMap();
		m1.put("Rajaraman","PerformanceTesting" );
		m1.put("Manish", "FunctionalTesting");
		m1.put("Ekta", "Mentor1");
		m1.put("Eeshan", "Mentor2");
		m1.put("Neeki","ManualTesting");
System.out.println(m1.keySet());
	}
}

Output:  [Eeshan, Ekta, Neeki, Rajaraman, Manish]

				
			

Screenshot:

h10

11. Let us check how  remove(Object key) method works in HashMap class.

Code Snippet:

				
					package hashMapclass;
import java.util.HashMap;
public class HashMapClass {
	public static void main(String[] args) {
		HashMap m1= new HashMap();
		m1.put("Rajaraman","Instructor2" );
		m1.put("Manish", "Instructor1");
		m1.put("Ekta", "Mentor1");
		m1.put("Eeshan", "Mentor2");
		m1.put("Neeki","Instructor3");
System.out.println(m1);
m1.remove("Rajaraman");
System.out.println("New key value pairs are:");
System.out.println(m1);
System.out.println(m1.remove("Deepesh"));
System.out.println(m1.remove("Manish"));
	}
}

Output: 
{Eeshan=Mentor2, Ekta=Mentor1, Neeki=Instructor3, Rajaraman=Instructor2, Manish=Instructor1}
New key value pairs are:
{Eeshan=Mentor2, Ekta=Mentor1, Neeki=Instructor3, Manish=Instructor1}
null
Instructor1

				
			

Screenshot:

h11

12. Let us check how  values() method works in HashMap class.

 Code Snippet:

				
					package hashMapclass;
import java.util.HashMap;
public class HashMapClass {
	public static void main(String[] args) {
		HashMap m1= new HashMap();
		m1.put("Rajaraman","Instructor2" );
		m1.put("Manish", "Instructor1");
		m1.put("Ekta", "Mentor1");
		m1.put("Eeshan", "Mentor2");
		m1.put("Neeki","Instructor3");
System.out.println(m1.values());
	}
}

     Output:  [Mentor2, Mentor1, Instructor3, Instructor2, Instructor1]

				
			

Screenshot:

h12

13. Let us check how  putIfAbsent(Object key, Object value) method works in HashMap class.

 

 Code Snippet:

				
					 package hashMapclass;
import java.util.HashMap;
public class HashMapClass {
	public static void main(String[] args) {
		HashMap m1= new HashMap();
		m1.put("Rajaraman","Instructor2" );
		m1.put("Manish", "Instructor1");
		m1.put("Ekta", "Mentor1");
		m1.put("Eeshan", "Mentor2");
		m1.put("Neeki","Instructor3");
		System.out.println(m1);
		System.out.println(m1.putIfAbsent("Rajaraman", "Instructor2"));
		System.out.println(m1);
System.out.println(m1.putIfAbsent("Deepesh","Instructor4"));
System.out.println(m1);
	}
}

Output: {Eeshan=Mentor2, Ekta=Mentor1, Neeki=Instructor3, Rajaraman=Instructor2, Manish=Instructor1}
Instructor2
{Eeshan=Mentor2, Ekta=Mentor1, Neeki=Instructor3, Rajaraman=Instructor2, Manish=Instructor1}
null
{Eeshan=Mentor2, Ekta=Mentor1, Neeki=Instructor3, Rajaraman=Instructor2, Deepesh=Instructor4, Manish=Instructor1}


				
			

Screenshot:

h13

14. Let us check how  replace(Object key, Object oldValue, Object newValue) method works in HashMap class.

Code Snippet:

				
					package hashMapclass;
import java.util.HashMap;
public class HashMapClass {
	public static void main(String[] args) {
		HashMap m1= new HashMap();
		m1.put("Rajaraman","Instructor2" );
		m1.put("Manish", "Instructor1");
		m1.put("Ekta", "Mentor1");
		m1.put("Eeshan", "Mentor2");
		m1.put("Neeki","Instructor3");
		System.out.println(m1);
		m1.replace("Rajaraman", "Instructor2", "SDET1");
System.out.println(m1);
	}
}
Output: {Eeshan=Mentor2, Ekta=Mentor1, Neeki=Instructor3, Rajaraman=Instructor2, Manish=Instructor1}
{Eeshan=Mentor2, Ekta=Mentor1, Neeki=Instructor3, Rajaraman=SDET1, Manish=Instructor1}

				
			

Screenshot:

h15

15. Let us check how  replace(Object key, Object value) method works in HashMap class.

Code Snippet:

				
					 package hashMapclass;
import java.util.HashMap;
public class HashMapClass {
	public static void main(String[] args) {
		HashMap m1= new HashMap();
		m1.put("Rajaraman","Instructor2" );
		m1.put("Manish", "Instructor1");
		m1.put("Ekta", "Mentor1");
		m1.put("Eeshan", "Mentor2");
		m1.put("Neeki","Instructor3");
		System.out.println(m1);
		m1.replace("Rajaraman", "SDET1");
System.out.println(m1);
	}
}

           Output:{Eeshan=Mentor2, Ekta=Mentor1, Neeki=Instructor3, Rajaraman=Instructor2, Manish=Instructor1}
{Eeshan=Mentor2, Ekta=Mentor1, Neeki=Instructor3, Rajaraman=SDET1, Manish=Instructor1}

				
			

Screenshot:

h151

16. Let us check how  entrySet() method works in HashMap class.

Code Snippet:

				
					package hashMapclass;
import java.util.HashMap;
public class HashMapClass {
	public static void main(String[] args) {
		HashMap m1= new HashMap();
		m1.put("Rajaraman","Instructor2" );
		m1.put("Manish", "Instructor1");
		m1.put("Ekta", "Mentor1");
		m1.put("Eeshan", "Mentor2");
		m1.put("Neeki","Instructor3");
		System.out.println(m1.entrySet());
		
	}
}

Output: [Eeshan=Mentor2, Ekta=Mentor1, Neeki=Instructor3, Rajaraman=Instructor2, Manish=Instructor1]
				
			

Screenshot:

h16

17. Let us check how  iterator() method works in HashMap class.

 Code Snippet:

				
					package hashMapclass;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
public class HashMapClass {
	public static void main(String[] args) {
		HashMap<String,String> m1= new HashMap<String,String>();
		m1.put("Rajaraman","Instructor2" );
		m1.put("Manish", "Instructor1");
		m1.put("Ekta", "Mentor1");
		m1.put("Eeshan", "Mentor2");
		m1.put("Neeki","Instructor3");
		Iterator<Entry<String, String>> itr= m1.entrySet().iterator();
		while(itr.hasNext())
		{
		System.out.println(itr.next());
		}	
		}
}

            Output: Eeshan=Mentor2
Ekta=Mentor1
Neeki=Instructor3
Rajaraman=Instructor2
Manish=Instructor1

				
			

Screenshot:

h17

18. Let us check how  clone() method works in HashMap class.

Code Snippet:

				
					 package hashMapclass;
import java.util.HashMap;
public class HashMapClass {
	public static void main(String[] args) {
		HashMap<String,String> m1= new HashMap<String,String>();
		m1.put("Rajaraman","Instructor2" );
		m1.put("Manish", "Instructor1");
		m1.put("Ekta", "Mentor1");
		m1.put("Eeshan", "Mentor2");
		m1.put("Neeki","Instructor3");
	System.out.println(m1);
	System.out.println(m1.clone());
		
	}
	
}

          Output: {Eeshan=Mentor2, Ekta=Mentor1, Neeki=Instructor3, Rajaraman=Instructor2, Manish=Instructor1}
{Eeshan=Mentor2, Rajaraman=Instructor2, Ekta=Mentor1, Manish=Instructor1, Neeki=Instructor3}

				
			

Screenshot:

h18

Difference between Map and HashMap

SL NO: Map HashMap
1 It is the topmost interface in Map hierarchy having lot of implemented classes and interfaces It is the implemented class of Map interface.
2 Map is slower than HashMap HashMap is faster than Map since it does not maintain insertion order of elements
3 Does not allow duplicate key and value pairs Allows duplicate key and value pairs.
4 Does not allow null keys and values Allows one null key and any number of null values

Conclusion:

HashMap class is used for handling all the key and value pairsin an effective way. It is necessary to understand the usage of all the methods of HashMap class for better handling of HashMap elements. By thoroughly understanding the HashMap class and its methods developers and testers can utitlise those concepts in their real life tasks. Remember to practise, stay updated with the latest trends in Automation Software Testing Course,and maintain a positive attitude throughout your interview process.Automation testers can utlize all the methods of HashMap class to create better and effective test scripts for automation testing of web applications and websites.

Also, read my blogs on:

Upskill Yourself
Consult Us