WeakHashMap keySet() in Java

keySet(): This method is available in java.util.WeakHashMap class of Java.

Syntax:

Set<K> java.util.WeakHashMap.keySet()

This 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.

Parameters: NA

Returns: a set view of the keys contained in this map.

Exceptions: NA

Approach

Java

import java.util.WeakHashMap;

public class WeakHashMapkeySet {
    public static void main(String[] args) {

        WeakHashMap<String, String> weakHashMap =
new WeakHashMap<String, String>();

        weakHashMap.put("Java", "Program");
        weakHashMap.put("Hello", "World");

        System.out.println(weakHashMap.keySet());

    }
}

Output:

[Hello, Java]


No comments:

Post a Comment