WeakHashMap get() Method in Java Last Updated : 19 Jul, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 the key whose associated value is supposed to be fetched. Return Value: The method returns the value associated with the key_element in the parameter. Below programs illustrates the working of java.util.WeakHashMap.get() method: Program 1: Java // Java code to illustrate the get() 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); // Getting the value of 25 System.out.println("The Value is: " + weak_hash.get(25)); // Getting the value of 10 System.out.println("The Value is: " + weak_hash.get(10)); } } Output: Initial Mappings are: {30=You, 15=4, 10=Geeks, 25=Welcomes, 20=Geeks} The Value is: Welcomes The Value is: Geeks Program 2: Java // Java code to illustrate the get() method import java.util.*; public class Weak_Hash_Map_Demo { public static void main(String[] args) { // Creating an empty WeakHashMap Map<String, Integer> weak_hash = new WeakHashMap<String, Integer>(); // Mapping int values to string keys weak_hash.put("Geeks", 10); weak_hash.put("4", 15); weak_hash.put("Geeks", 20); weak_hash.put("Welcomes", 25); weak_hash.put("You", 30); // Displaying the WeakHashMap System.out.println("Initial Mappings are: " + weak_hash); // Getting the value of "Geeks" System.out.println("The Value is: " + weak_hash.get("Geeks")); // Getting the value of "You" System.out.println("The Value is: " + weak_hash.get("You")); } } Output: Initial Mappings are: {Welcomes=25, 4=15, You=30, Geeks=20} The Value is: 20 The Value is: 30 Note: The same operation can be performed with any type of Mappings with variation and combination of different data types. Comment More infoAdvertise with us Next Article WeakHashMap entrySet() Method in Java C chinmoy lenka Follow Improve Article Tags : Misc Java Java-Collections Java-Functions Java-WeakHashMap +1 More Practice Tags : JavaJava-CollectionsMisc Similar Reads 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 illus 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 doe 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 3 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 2 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 met 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_ 2 min read Like