Open In App

WeakHashMap remove() method in Java

Last Updated : 20 Jul, 2018
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The java.util.WeakHashMap.remove() is an inbuilt method of WeakHashMap 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:

Weak_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.WeakHashMap.remove() method:
Program 1: When passing an existing key.




// Java code to illustrate the remove() method
import java.util.*;
  
public class Weak_Hash_Map_Demo {
    public static void main(String[] args)
    {
  
        // Creating an empty WeakHashMap
        Map<Integer, String> weak_hash = new 
                       WeakHashMap<Integer, String>();
    
        // Mapping string values to int keys
        weak_hash.put(10, "Geeks");
        weak_hash.put(15, "4");
        weak_hash.put(20, "Geeks");
        weak_hash.put(25, "Welcomes");
        weak_hash.put(30, "You");
  
        // Displaying the WeakHashMap
        System.out.println("Initial Mappings are: "
                                           weak_hash);
  
        // Removing the existing key mapping
        String returned_value = 
                         (String)weak_hash.remove(20);
  
        // Verifying the returned value
        System.out.println("Returned value is: "
                                      returned_value);
  
        // Displayin the new map
        System.out.println("New map is: " + weak_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.




// Java code to illustrate the remove() method
import java.util.*;
  
public class Weak_Hash_Map_Demo {
    public static void main(String[] args)
    {
  
        // Creating an empty WeakHashMap
        Map<Integer, String> weak_hash = 
                   new WeakHashMap<Integer, String>();
  
        // Mapping string values to int keys
        weak_hash.put(10, "Geeks");
        weak_hash.put(15, "4");
        weak_hash.put(20, "Geeks");
        weak_hash.put(25, "Welcomes");
        weak_hash.put(30, "You");
  
        // Displaying the WeakHashMap
        System.out.println("Initial Mappings are: "
                                          weak_hash);
  
        // Removing the new key mapping
        String returned_value = 
                        (String)weak_hash.remove(50);
  
        // Verifying the returned value
        System.out.println("Returned value is: "
                                    returned_value);
  
        // Displayin the new map
        System.out.println("New map is: "+weak_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.



Similar Reads

WeakHashMap keySet() method in Java
The java.util.WeakHashMap.keySet() method in Java is used to create a set out of the key elements contained in the hash map. It basically returns a set view of the keys or we can create a new set and store the key elements in them. Syntax: Weak_Hash_Map.keySet() Parameters: The method does not take any parameter. Return Value: The method returns a
2 min read
WeakHashMap clear() Method in Java
The java.util.WeakHashMap.clear() method in Java is used to clear and remove all of the elements or mappings from a specified map. Syntax: Weak_Hash_Map.clear() Parameters: The method does not accept any parameters. Return Value: The method does not return any value. Below programs are used to illustrate the working of java.util.WeakHashMap.clear()
2 min read
WeakHashMap containsKey() Method in Java
The java.util.WeakHashMap.containsKey() method is used to check whether a particular key is being mapped into the WeakHashMap or not. It takes the key element as a parameter and returns True if that element is mapped in the map. Syntax: Weak_Hash_Map.containsKey(key_element) Parameters: The method takes just one parameter key_element that refers to
2 min read
WeakHashMap containsValue() Method in Java
The java.util.WeakHashMap.containsValue() method is used to check whether a particular value is being mapped by a single or more than one key in the WeakHashMap. It takes the Value as a parameter and returns True if that value is mapped by any of the key in the map. Syntax: Weak_Hash_Map.containsValue(Object Value) Parameters: The method takes just
2 min read
WeakHashMap entrySet() Method in Java
The java.util.WeakHashMap.entrySet() method in Java is used to create a set out of the same elements contained in the map. It basically returns a set view of the weak hash map or we can create a new set and store the map elements into them. Syntax: weak_hash_map.entrySet() Parameters: The method does not take any parameter. Return Value: The method
2 min read
WeakHashMap isEmpty() Method in Java with Examples
The java.util.WeakHashMap.isEmpty() method of HashMap class is used to check for the emptiness of the map. The method returns True if no key-value pair or mapping is present in the map else False. Syntax: Weak_Hash_Map.isEmpty() Parameters: The method does not take any parameters. Return Value: The method returns boolean true if the map is empty or
2 min read
WeakHashMap get() Method in Java
The java.util.WeakHashMap.get() method of WeakHashMap class is used to retrieve or fetch the value mapped by a particular key mentioned in the parameter. It returns NULL when the map contains no such mapping for the key. Syntax: WeakHashMap.get(Object key_element) Parameter: The method takes one parameter key_element of object type and refers to th
2 min read
WeakHashMap putall() Method in Java
The java.util.WeakHashMap.putAll() is an inbuilt method of WeakHashMap class that is used for the copy operation. The method copies all of the elements i.e., the mappings, from one map into another. Syntax: new_weakhash_map.putAll(exist_weakhash_map) Parameters: The method takes one parameter exist_weakhash_map that refers to the existing map we wa
2 min read
WeakHashMap put() Method in Java
The java.util.WeakHashMap.put() method of WeakHashMap is used to insert a mapping into a map. This means we can insert a specific key and the value it is mapping, into a particular map. If an existing key is passed then the previous value gets replaced by the new value. If a new pair is passed, then the pair gets inserted as a whole. Syntax: Weak_H
3 min read
WeakHashMap values() Method in Java
The java.util.WeakHashMap.values() method of HashMap class in Java is used to create a collection out of the values of the map. It basically returns a Collection view of the values in the Map. Syntax: Weak_Hash_Map.values() Parameters: The method does not accept any parameters. Return Value: The method is used to return a collection view containing
2 min read
WeakHashMap size() method in Java
The java.util.WeakHashMap.size() method of WeakHashMap class is used to get the size of the map which refers to the number of the key-value pair or mappings in the Map. Syntax: WeaK_Hash_Map.size() Parameters: The method does not take any parameters. Return Value: The method returns the size of the map which also means the number of key-value pairs
2 min read
Difference Between IdentityHashMap, WeakHashMap, and EnumMap in Java
The IdentityHashMap, WeakHashMap, and EnumMap all are the classes in java collection that implements the Map interface. But there are few differences exists between them. 1. IdentityHashMap: The IdentityHashMap implements the Map interface. It follows reference-equality in place of object-equality when comparing keys (and values). This class is use
5 min read
WeakHashMap Class in Java
WeakHashMap is an implementation of the Map interface. WeakHashMap is almost the same as HashMap except in the case of WeakHashMap if the object is specified as the key doesn’t contain any references- it is eligible for garbage collection even though it is associated with WeakHashMap. i.e Garbage Collector dominates over WeakHashMap. WeakHashMap is
7 min read
TreeSet remove() Method in Java
The Java.util.TreeSet.remove(Object O) method is to remove a particular element from a Tree set. Syntax: TreeSet.remove(Object O) Parameters: The parameter O is of the type of Tree set and specifies the element to be removed from the set. Return Value: This method returns True if the element specified in the parameter is initially present in the Se
1 min read
EnumMap remove() Method in Java
The Java.util.EnumMap.remove(key) method in Java is used to remove the specified key from the map. Syntax: remove(Object key) Parameters: The method takes one parameter key which refers to the key whose mapping is to be removed. Return Value: The method does not return any value. Below programs illustrate the working of remove(key) function: Progra
2 min read
LinkedList remove() Method in Java
LinkedList as we all know is a way of storing data that contains sets of nodes where each node contains data and address part where address part is responsible for linking of nodes and hence forming a List over which now we can perform operations. Now here we want to remove a node/s using the remove() method of LinkedList class only. Illustration:
4 min read
HashSet remove() Method in Java
HashSet remove() method is used to remove a particular element from a HashSet. Note that it is only after JDK version 1.2 and ahead, and will throw compilation errors before in version JDK 1 and JDK1.1. Note: This method returns true if the specified element is present in the HashSet otherwise it returns boolean false. Syntax: HashSet.remove(Object
2 min read
PriorityQueue remove() Method in Java
The remove() method of PriorityQueue class of java.util package is used to remove a particular element from a PriorityQueue. As we all know that the elements while entering into the priority queue are not sorted but as we all know while taking out elements from the priority queue the elements are always sorted being a trait of the priority queue. H
5 min read
HashMap remove() Method in Java
The java.util.HashMap.remove() is an inbuilt method of HashMap 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: Hash_Map.remove(Object key) Parameters: The method takes one parameter key whose mapping is to be removed from the Map.Return Value: The
2 min read
TreeMap remove() Method in Java
The java.util.TreeMap.remove() is an inbuilt method of TreeMap 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: Tree_Map.remove(Object key) Parameters: The method takes one parameter key whose mapping is to be removed from the Map. Return Value: Th
2 min read
ArrayDeque remove() Method in Java
The Java.util.ArrayDeque.remove() method is used to remove the element present at the head of the Deque. Syntax: Array_Deque.remove() Parameters: The method does not take any parameters. Return Value: This method returns the element present at the head of the Deque. Exceptions: The method throws NoSuchElementException is thrown if the deque is empt
3 min read
ArrayBlockingQueue remove() method in Java
ArrayBlockingQueue is bounded, blocking queue that stores the elements internally backed by an array. ArrayBlockingQueue class is a member of the Java Collections Framework. Bounded means it will have a fixed size, you can not store number the elements more than the capacity of the queue. The queue also follows FIFO (first-in-first-out) rule for st
3 min read
IdentityHashMap remove() Method in Java
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
2 min read
Vector remove() Method in Java
remove(int index) The java.util.vector.remove(int index) method is used to remove an element from a Vector from a specific position or index. Syntax: Vector.remove(int index) Parameters: This method accepts a mandatory parameter index is of integer data type and specifies the position of the element to be removed from the Vector. Return Value: This
2 min read
LinkedBlockingQueue remove() method in Java
The remove(Object obj) method of LinkedBlockingQueue removes only one instance of the given Object, passed as parameter, from this LinkedBlockingQueue if it is present. It removes an element e such that obj.equals(e) and if this queue contains one or more instance of element e. This method returns true if this queue contained the element which is n
4 min read
PriorityBlockingQueue remove() method in Java
The remove(Object o) method of PriorityBlockingQueue is used to delete an element from this queue. This method removes a single instance of the element passed as the parameter, if it is present. It returns true if and only if the element was removed, else it returned false. Syntax: public boolean remove(Object o) Parameter: This method accepts a ma
2 min read
ConcurrentHashMap remove() method in Java
The remove() method in Java's ConcurrentHashMap class is used to remove the entry for a given key from the map. It has the following signature: V remove(Object key)where: key is the key of the entry to be removed.V is the type of values in the map.The remove() method works in a concurrent environment, which means that multiple threads can access an
7 min read
LinkedTransferQueue remove() method in Java
The java.util.concurrent.LinkedTransferQueue.remove() method is an in-built function in Java which is used to remove an element if it is present in this queue. Syntax: LinkedTransferQueue.remove(Object o) Parameters: The function accepts a single parameter o i.e. the object to be removed. Return Value: The function returns a true boolean value on s
2 min read
ConcurrentSkipListSet remove() method in Java
The java.util.concurrent.ConcurrentSkipListSet.remove() method is an in-built function in Java which is used to remove an element if it is present in this set. Syntax: ConcurrentSkipListSet.remove(Object o) Parameters: The function accepts a single parameter o i.e. the object to be removed. Return Value: The function returns a true boolean value on
2 min read
LinkedBlockingDeque remove() method in Java
The remove() method of LinkedBlockingDeque removes the head of the Deque container. The method throws a NoSuchElementException if the Deque container is empty. Syntax: public E remove() Parameters: This method does not accepts any parameter. Returns: This method does not returns anything. Exception: The function throws a NoSuchElementException if t
2 min read
three90RightbarBannerImg