The java.util.IdentityHashMap.remove() is an inbuilt method of IdentityHashMap class and is used to remove the mapping of any particular key from the map. It basically removes the values for any particular key in the Map.
Syntax:
Identity_Hash_Map.remove(Object key)
Parameters: The method takes one parameter key whose mapping is to be removed from the Map.
Return Value: The method returns the value that was previously mapped to the specified key if the key exists else the method returns NULL.
Below programs illustrates the working of java.util.IdentityHashMap.remove() method:
Program 1: When passing an existing key.
import java.util.*;
public class Identity_Hash_Map_Demo {
public static void main(String[] args)
{
Map<Integer, String> Identity_hash = new
IdentityHashMap<Integer, String>();
Identity_hash.put( 10 , "Geeks" );
Identity_hash.put( 15 , "4" );
Identity_hash.put( 20 , "Geeks" );
Identity_hash.put( 25 , "Welcomes" );
Identity_hash.put( 30 , "You" );
System.out.println( "Initial Mappings are: " +
Identity_hash);
String returned_value =
(String)Identity_hash.remove( 20 );
System.out.println( "Returned value is: " +
returned_value);
System.out.println( "New map is: " + Identity_hash);
}
}
|
Output:
Initial Mappings are: {30=You, 15=4, 10=Geeks, 25=Welcomes, 20=Geeks}
Returned value is: Geeks
New map is: {30=You, 15=4, 10=Geeks, 25=Welcomes}
Program 2: When passing a new key.
import java.util.*;
public class Identity_Hash_Map_Demo {
public static void main(String[] args)
{
Map<Integer, String> Identity_hash =
new IdentityHashMap<Integer, String>();
Identity_hash.put( 10 , "Geeks" );
Identity_hash.put( 15 , "4" );
Identity_hash.put( 20 , "Geeks" );
Identity_hash.put( 25 , "Welcomes" );
Identity_hash.put( 30 , "You" );
System.out.println( "Initial Mappings are: " +
Identity_hash);
String returned_value =
(String)Identity_hash.remove( 50 );
System.out.println( "Returned value is: " +
returned_value);
System.out.println( "New map is: " +Identity_hash);
}
}
|
Output:
Initial Mappings are: {30=You, 15=4, 10=Geeks, 25=Welcomes, 20=Geeks}
Returned value is: null
New map is: {30=You, 15=4, 10=Geeks, 25=Welcomes, 20=Geeks}
Note: The same operation can be performed with any type of Mappings with variation and combination of different data types.
Feeling lost in the vast world of Backend Development? It's time for a change! Join our
Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
- Comprehensive Course
- Expert Guidance for Efficient Learning
- Hands-on Experience with Real-world Projects
- Proven Track Record with 100,000+ Successful Geeks
Last Updated :
24 Jul, 2018
Like Article
Save Article