GroTechMinds

WeakHashMap and its methods (1)

WeakHashMap and its methods

Before going into WeakHashMap let us discuss about Map Hierarchy

Map Hierarchy

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

Map Table

Now let us discuss about WeakHashMap  and its methods

What is WeakHashMap?

WeakHashMap is the implemented class of Map interface. It is the Hastable based implementation of Map interface having weak keys. Any element in WeakHashMap will be automatically removed from the WeakHashMap class if the key is no longer in use. WeakHashMap accepts null keys and null values. It is neither threadsafe nor synchronized as a result it has faster execution. It increases its capacity when it reaches the load factor value of 0.75. Its threshold is 0.75 times the current capacity.

Methods of WeakHashMap

Below are some of the methods of WeakHashMap:

Method Name Description
put(Object key, Object value) It is used to add a pair of key and value into the HashHap.
putAll(Map m) It is used to add all pairs of keys and values from one WeakHashMap to another WeakHashMap.
clear() It is used to clear all key and value pairs present in WeakHashMap.
size() It is used to display the size of the key and value pairs present in WeakHashMap.
equals(Object o) It is used to check if key and value pairs present in one WeakHashMap is equal to the key and value pairs present in another WeakHashMap. It gives ‘true’ value if key and value pairs present in two WeakHashMaps are equal and ‘false’ value if key and value pairs present in two WeakHashMaps 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 WeakHashMap 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 WeakHashMap 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 WeakHashMap.
isEmpty() It is used to check if the WeakHashMap class is empty or not. It display ‘true’ value if the WeakHashMap class is empty and ‘false’ value if the WeakHashMap class is not empty.
keySet() It is used to display the keys among all the key and value pairs present in WeakHashMap.
remove(Object key) It is used to remove a specified key and value pair from the WeakHashMap. If we try to print a key which is not present in WeakHashMap it will display null value but if we try to print a key which is present in WeakHashMap it will display its corresponding value.
values() It is used to display the corresponding values of the keys present in the WeakHashMap
putIfAbsent(Object key, Object value) Using this method, the specified key and value pairs get added alongwith the key and value pairs present in WeakHashMap if that key and value pair is absent in the WeakHashMap. If the specified key and value is present in the WeakHashMap the specified key and value pair will not get added again in the WeakHashMap. This method will also display the value of the specified key and value pair if that key and value pair is present in WeakHashMap. If the specified key and value pair is not displayed in WeakHashMap 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 WeakHashMap.
iterator() It is used to iterate sequentially all the key and value pairs present in WeakHashMap.

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

Code Snippet:

				
					  package weakHashMapclass;
      import java.util.WeakHashMap;
      public class WeakHashMapclass {
	 public static void main(String[] args) {
		WeakHashMap m1= new WeakHashMap();
    m1.put("Roll No",1101219512 );
	 m1.put("Registration Number", 1653005);
	 m1.put("Pin Number", 3434);
	 m1.put("Student ID",25 );
	 m1.put("Postal code",751121);
      System.out.println(m1);
 	}
   }
Output: {Student ID=25, Pin Number=3434, Roll No=1101219512, Postal code=751121, Registration Number=1653005}

				
			

Screenshot:

m1

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

Code Snippet:

				
					package weakHashMapclass;
     import java.util.WeakHashMap;
     public class WeakHashMapclass {
	public static void main(String[] args) {
		WeakHashMap m1= new WeakHashMap();
     m1.put("Roll No",1101219512 );
	 m1.put("Registration Number", 1653005);
	 m1.put("Pin Number", 3434);
	 m1.put("Student ID",25 );
	 m1.put("Postal code",751121);


  System.out.println(m1);
WeakHashMap m2= new WeakHashMap();
m2.putAll(m1);
System.out.println(m2);


	}
}

Output:  {Student ID=25, Pin Number=3434, Roll No=1101219512, Postal code=751121, Registration Number=1653005}
{Student ID=25, Roll No=1101219512, Pin Number=3434, Postal code=751121, Registration Number=1653005}

				
			

Screenshot:

m2

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

Code Snippet:

				
					package weakHashMapclass;
import java.util.WeakHashMap;
public class WeakHashMapclass {
	public static void main(String[] args) {
		WeakHashMap m1= new WeakHashMap();
  m1.put("Roll No",1101219512 );
	 m1.put("Registration Number", 1653005);
	 m1.put("Pin Number", 3434);
	 m1.put("Student ID",25 );
	 m1.put("Postal code",751121);


m1.clear();
System.out.println(m1);
	}
}


       Output: {}

				
			

 Screenshot:

m3

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

Code Snippet:

				
					package weakHashMapclass;
import java.util.WeakHashMap;
public class WeakHashMapclass {
	public static void main(String[] args) {
		WeakHashMap m1= new WeakHashMap();
		  m1.put("Roll No",1101219512 );
	 m1.put("Registration Number", 1653005);
	 m1.put("Pin Number", 3434);
	 m1.put("Student ID",25 );
	 m1.put("Postal code",751121);


		System.out.println(m1.size());
	}
}

Output: 5

				
			

Screenshot:

m4

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

Code Snippet:

				
					package weakHashMapclass;
import java.util.WeakHashMap;
public class WeakHashMapclass {
	public static void main(String[] args) {
		WeakHashMap m1= new WeakHashMap();
		  m1.put("Roll No",1101219512 );
	 m1.put("Registration Number", 1653005);
	 m1.put("Pin Number", 3434);
	 m1.put("Student ID",25 );
	 m1.put("Postal code",751121);


		WeakHashMap m2= new WeakHashMap();
		  m2.put("Roll No",1101219512 );
	 m2.put("Registration Number", 1653005);
	 m2.put("Pin Number", 3434);
	 m2.put("Student ID",25 );
	 m2.put("Postal code",751121);


		WeakHashMap m3= new WeakHashMap();
	  m3.put("Roll No",1101219512 );
	 m3.put("Registration Number", 1653005);
	 m3.put("Pin Number", 3434);
	 m3.put("Student ID",25 );
	 m3.put("Postal code",751121);


		m3.put("Flat No",01);
		System.out.println("Is WeakHashMap m1 equals m2 ?: "+m1.equals(m2));
		System.out.println("Is WeakHashMap m1 equals m3 ?: "+m1.equals(m3));
	}
}

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



				
			

Screenshot:

m5

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

Code Snippet:  

				
					package weakHashMapclass;
import java.util.WeakHashMap;
public class WeakHashMapclass {
	public static void main(String[] args) {
		WeakHashMap m1= new WeakHashMap();
		  m1.put("Roll No",1101219512 );
	 m1.put("Registration Number", 1653005);
	 m1.put("Pin Number", 3434);
	 m1.put("Student ID",25 );
	 m1.put("Postal code",751121);


		System.out.println("Does the WeakHashMap contains Registration Number key?: "+m1.containsKey("Registration Number"));
System.out.println("Does the WeakHashMap contains Flat No key?: "+m1.containsKey("Flat No"));
		


	}
}

          Output: Does the WeakHashMap contains Registration Number key?: true
Does the WeakHashMap contains Flat No key?: false

				
			

Screenshot:

m6

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

Code Snippet:  

				
					package weakHashMapclass;
import java.util.WeakHashMap;
public class WeakHashMapclass {
	public static void main(String[] args) {
		WeakHashMap m1= new WeakHashMap();
		  m1.put("Roll No",1101219512 );
	 m1.put("Registration Number", 1653005);
	 m1.put("Pin Number", 3434);
	 m1.put("Student ID",25 );
	 m1.put("Postal code",751121);


System.out.println("Does the WeakHashMap contains 1653005 value?: "+m1.containsValue(1653005));
System.out.println("Does the WeakHasMap contains 01 value?: "+m1.containsValue(01));
	}
}

Output:  Does the WeakHashMap contains 1653005 value?: true
Does the HasMap contains 01 value?: false

				
			

Screenshot:  

m7

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

Code Snippet:  

				
					 package weakHashMapclass;
import java.util.WeakHashMap;
public class WeakHashMapclass {
	public static void main(String[] args) {
		WeakHashMap m1= new WeakHashMap();
		 m1.put("Roll No",1101219512 );
	 m1.put("Registration Number", 1653005);
	 m1.put("Pin Number", 3434);
	 m1.put("Student ID",25 );
	 m1.put("Postal code",751121);


System.out.println(m1.get("Pin Number"));
System.out.println(m1.get("Registration Number"));
		


	}
}

Output:
3434
1653005

				
			

Screenshot:

m8

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

Code Snippet:  

				
					package weakHashMapclass;
import java.util.WeakHashMap;
public class WeakHashMapclass {
	public static void main(String[] args) {
		WeakHashMap m1= new WeakHashMap();
		 m1.put("Roll No",1101219512 );
	 m1.put("Registration Number", 1653005);
	 m1.put("Pin Number", 3434);
	 m1.put("Student ID",25 );
	 m1.put("Postal code",751121);


System.out.println("Is WeakHashMap m1 empty? :" +m1.isEmpty());
WeakHashMap m2= new WeakHashMap();
System.out.println("Is WeakHashMap m2 empty? :" +m2.isEmpty());
	}
}
Output: Is WeakHashMap m1 empty? :false
Is WeakHashMap m2 empty? :true

				
			

Screenshot:

m9

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

Code Snippet:  

				
					package weakHashMapclass;
import java.util.WeakHashMap;
public class WeakHashMapclass {
	public static void main(String[] args) {
		WeakHashMap m1= new WeakHashMap();
		 m1.put("Roll No",1101219512 );
	 m1.put("Registration Number", 1653005);
	 m1.put("Pin Number", 3434);
	 m1.put("Student ID",25 );
	 m1.put("Postal code",751121);


System.out.println(m1.keySet());
	}
}

Output:  [Student ID, Pin Number, Roll No, Postal code, Registration Number]

				
			

Screenshot:

				
					console.log( 'Code is Poetry' );
				
			

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

Code Snippet:

				
					package weakHashMapclass;
import java.util.WeakHashMap;
public class WeakHashMapclass {
	public static void main(String[] args) {
		WeakHashMap m1= new WeakHashMap();
		 m1.put("Roll No",1101219512 );
	 m1.put("Registration Number", 1653005);
	 m1.put("Pin Number", 3434);
	 m1.put("Student ID",25 );
	 m1.put("Postal code",751121);


System.out.println(m1);
m1.remove("Registration Number");
System.out.println("New key value pairs are:");
System.out.println(m1);
System.out.println(m1.remove("Flat No"));
System.out.println(m1.remove("Student ID"));
	}
}

Output: 
{Student ID=25, Pin Number=3434, Roll No=1101219512, Postal code=751121, Registration Number=1653005}
New key value pairs are:
{Student ID=25, Pin Number=3434, Roll No=1101219512, Postal code=751121}
null
25

				
			

Screenshot:

j11

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

Code Snippet:

				
					   package weakHashMapclass;
import java.util.WeakHashMap;
public class WeakHashMapclass {
	public static void main(String[] args) {
		WeakHashMap m1= new WeakHashMap();
			 m1.put("Roll No",1101219512 );
	 m1.put("Registration Number", 1653005);
	 m1.put("Pin Number", 3434);
	 m1.put("Student ID",25 );
	 m1.put("Postal code",751121);


System.out.println(m1.values());
	}
}

     Output:  [25, 3434, 1101219512, 751121, 1653005]

				
			

 Screenshot:

j12

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

Code Snippet:

				
					package weakHashMapclass;
import java.util.WeakHashMap;
public class WeakHashMapclass {
	public static void main(String[] args) {
		WeakHashMap m1= new WeakHashMap();
			 m1.put("Roll No",1101219512 );
	 m1.put("Registration Number", 1653005);
	 m1.put("Pin Number", 3434);
	 m1.put("Student ID",25 );
	 m1.put("Postal code",751121);
      System.out.println(m1);


		System.out.println(m1.putIfAbsent("Registration Number", 1653005));
		System.out.println(m1);
System.out.println(m1.putIfAbsent("Flat No",01));
System.out.println(m1);
	}
}

Output: {Student ID=25, Pin Number=3434, Roll No=1101219512, Postal code=751121, Registration Number=1653005}
1653005
{Student ID=25, Pin Number=3434, Roll No=1101219512, Postal code=751121, Registration Number=1653005}
null
{Student ID=25, Pin Number=3434, Roll No=1101219512, Flat No=1, Postal code=751121, Registration Number=1653005}

				
			

Screenshot:

j13

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

Code Snippet:

				
					package weakHashMapclass;
import java.util.WeakHashMap;
public class WeakHashMapclass {
	public static void main(String[] args) {
		WeakHashMap m1= new WeakHashMap();
		 m1.put("Roll No",1101219512 );
	 m1.put("Registration Number", 1653005);
	 m1.put("Pin Number", 3434);
	 m1.put("Student ID",25 );
	 m1.put("Postal code",751121);


		System.out.println(m1);
		m1.replace("Registration Number", 1653005, 1653007);
System.out.println(m1);
	}
}
Output: {Student ID=25, Pin Number=3434, Roll No=1101219512, Postal code=751121, Registration Number=1653005}
{Student ID=25, Pin Number=3434, Roll No=1101219512, Postal code=751121, Registration Number=1653007}

				
			

Screenshot:

j14

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

Code Snippet:

				
					package weakHashMapclass;
import java.util.WeakHashMap;
public class WeakHashMapclass {
	public static void main(String[] args) {
		WeakHashMap m1= new WeakHashMap();
		 m1.put("Roll No",1101219512 );
	 m1.put("Registration Number", 1653005);
	 m1.put("Pin Number", 3434);
	 m1.put("Student ID",25 );
	 m1.put("Postal code",751121);


		System.out.println(m1);
		m1.replace("Registration Number", 1653005);
System.out.println(m1);
	}
}

           Output:{Student ID=25, Pin Number=3434, Roll No=1101219512, Postal code=751121, Registration Number=1653005}
{Student ID=25, Pin Number=3434, Roll No=1101219512, Postal code=751121, Registration Number=1653005}


				
			

Screenshot:

j15

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

 

Code Snippet:

				
					   package weakHashMapclass;
import java.util.WeakHashMap;
public class WeakHashMapclass {
	public static void main(String[] args) {
		WeakHashMap m1= new WeakHashMap();
		 m1.put("Roll No",1101219512 );
	 m1.put("Registration Number", 1653005);
	 m1.put("Pin Number", 3434);
	 m1.put("Student ID",25 );
	 m1.put("Postal code",751121);


		System.out.println(m1.entrySet());
		
	}
}

Output: [Student ID=25, Pin Number=3434, Roll No=1101219512, Postal code=751121, Registration Number=1653005]


				
			

Screenshot:

j16

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

 

Code Snippet:

				
					 package weakHashMapclass;
import java.util.WeakHashMap;
import java.util.Iterator;
import java.util.Map.Entry;
public class WeakHashMapclass {
	public static void main(String[] args) {
		WeakHashMap <String,Integer> m1= new WeakHashMap<String,Integer>();
		 m1.put("Roll No",1101219512 );
	 m1.put("Registration Number", 1653005);
	 m1.put("Pin Number", 3434);
	 m1.put("Student ID",25 );
	 m1.put("Postal code",751121);






		Iterator<Entry<String, String>> itr= m1.entrySet().iterator();
		while(itr.hasNext())
		{
		System.out.println(itr.next());
		}	
		}
}

            Output: Student ID=25
Pin Number=3434
Roll No=1101219512
Postal code=751121
Registration Number=1653005

				
			

Screenshot:

Difference between HashMap and WeakHashMap

SL NO: HashMap WeakHashMap
1 Stores strong references for its keys. Stores weak references for its keys.
2 Unless Maps are referenced keys cannot be garbage collected. Keys can be garbage collected due to storage of weak reference.
3 Iteration behaviour reflects the present state of Map. Iteration behaviour reflects state of Map of last garbage collection.

Conclusion:

WeakHashMap class is used for handling all the key and value pairs in an effective way. It is necessary to understand the usage of all the methods of the WeakHashMap class for better handling of WeakHashMap elements. By thoroughly understanding the WeakHashMap 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 WeakHashMap 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