Java WeakHashMap - keySet() Method
The java.util.WeakHashMap.keySet() method returns a Set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa.
Syntax
public Set<K> keySet()
Here, K is the type of key maintained by the container.
Parameters
No parameter is required.
Return Value
Returns a set view of the keys contained in this map.
Exception
NA
Example:
In the example below, the java.util.WeakHashMap.keySet() method returns a set view of the keys contained in the given WeakHashMap.
import java.util.*; public class MyClass { public static void main(String[] args) { //creating a weakhashmap WeakHashMap<Integer, String> MyMap = new WeakHashMap<Integer, String>(); //populating the map MyMap.put(101, "John"); MyMap.put(102, "Marry"); MyMap.put(103, "Kim"); MyMap.put(104, "Jo"); MyMap.put(105, "Sam"); //printing the content of the map System.out.println("MyMap contains: " + MyMap); //creating a set view of keys of the map Set SetView = MyMap.keySet(); //printing the set view of keys System.out.println("SetView contains: " + SetView); } }
The output of the above code will be:
MyMap contains: {105=Sam, 104=Jo, 101=John, 103=Kim, 102=Marry} SetView contains: [105, 104, 101, 103, 102]
❮ Java.util - WeakHashMap