This program demonstrates how to use the remove method of the HashMap class to remove a key-value pair from a HashMap.
First, a HashMap object is created and some key-value pairs are added to it using the put method. Then, a for loop is used to iterate through the key-value pairs in the HashMap and print them to the console.
Next, the remove method is used to remove a key-value pair from the HashMap. The key 1002 is used as an argument to remove the key-value pair associated with it.
Finally, another for loop is used to iterate through the key-value pairs in the HashMap after the removal operation and print them to the console. This demonstrates that the key-value pair associated with the key 1002 has been successfully removed from the HashMap.
import java.util.*; public class Remove_Method { public static void main(String args[]) { HashMap<Integer,String> h =new HashMap<>(); h.put(1000,"Sam"); h.put(1001,"Sathish"); h.put(1002,"Kumar"); h.put(1003,"Ram"); h.put(1004,"Pooja"); for(Map.Entry e : h.entrySet()) { System.out.println("Key : "+ e.getKey()+" || Value : "+e.getValue()); } h.remove(1002);//Remove the key-value pair System.out.println("\nAfter Remove the key-value pair elements in HashMap .. \n"); for(Map.Entry e : h.entrySet()) { System.out.println("Key : "+ e.getKey()+" || Value : "+e.getValue()); } } }
Key : 1000 || Value : Sam Key : 1001 || Value : Sathish Key : 1002 || Value : Kumar Key : 1003 || Value : Ram Key : 1004 || Value : Pooja After Remove the key-value pair elements in HashMap .. Key : 1000 || Value : Sam Key : 1001 || Value : Sathish Key : 1003 || Value : Ram Key : 1004 || Value : Pooja
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions