java.util.WeakHashMap.keySet() 方法
描述
keySet() 方法用于返回此映射中包含的键的集合视图。 集合由映射支持,因此对映射的更改会反映在集合中,反之亦然。
声明
以下是 java.util.WeakHashMap.keySet() 方法的声明。
public Set<K> keySet()
参数
NA
返回值
该方法调用返回此映射中包含的键的集合视图。
异常
NA
示例
下面的例子展示了 java.util.WeakHashMap.keySet() 方法的使用。
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);
}
}
让我们编译并运行上面的程序,这将产生以下结果.
Contents of the Map {1=first, 2=two, 3=three} Contents of the set [1, 2, 3]