java.util.WeakHashMap.get() 方法返回指定键映射到的值,如果map不包含该键所对应的值,则返回 null。
返回值 null 并不一定表示该map不包含该键的所对应的值;因为map也可能显式地将键对应的值设为为 null。
语法
public V get(Object key)
这里,V是容器维护的值的类型。
参数
key | 指定要返回关联值的key。 |
返回值
返回指定键映射到的值,如果此map不包含该键所对应的值,则返回 null。
异常
无示例:
在下面的示例中,java.util.WeakHashMap.get() 方法返回给定键中映射到的值map。
import java.util.*;
public class MyClass {
public static void main(String[] args) {
//创建weakhashmap
WeakHashMap<Integer, String> MyMap = new WeakHashMap<Integer, String>();
//填充map
MyMap.put(101, "John");
MyMap.put(102, "Marry");
MyMap.put(103, "Kim");
MyMap.put(104, "Jo");
//打印指定键的映射值
System.out.println("101 is mapped to: " + MyMap.get(101));
System.out.println("102 is mapped to: " + MyMap.get(102));
System.out.println("103 is mapped to: " + MyMap.get(103));
System.out.println("104 is mapped to: " + MyMap.get(104));
}
}
上述代码的输出将是:
101 is mapped to: John
102 is mapped to: Marry
103 is mapped to: Kim
104 is mapped to: Jo