java.util.WeakHashMap.keySet() Method


Advertisements


Description

The keySet() method is used to return 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.

Declaration

Following is the declaration for java.util.WeakHashMap.keySet() method.

public Set<K> keySet()

Parameters

  • NA

Return Value

The method call returns a set view of the keys contained in this map.

Exception

  • NA

Example

The following example shows the usage of java.util.WeakHashMap.keySet() method.

package com.tutorialspoint;

import java.util.Map;
import java.util.WeakHashMap;
import java.util.Set;

public class WeakHashMapDemo {
   public static void main(String[] args) { 
   Map<String, String> weakHashMap = new WeakHashMap<String, String>();
      
   // put keys and values in the Map      
   weakHashMap.put("1", "first");
   weakHashMap.put("2", "two");
   weakHashMap.put("3", "three");
      
   // printing the contents of the Map
   System.out.println("Contents of the Map");
   System.out.println(weakHashMap);
      
   // populating the set
   Set set = weakHashMap.keySet();
   System.out.println("Contents of the set");
   System.out.println(set);
   }     
}

Let us compile and run the above program, this will produce the following result.

Contents of the Map
{1=first, 2=two, 3=three}
Contents of the set
[1, 2, 3]

java_util_weakhashmap.htm

Advertisements