Java.util.Hashtable 类

java.util.Hashtable.get() 方法返回指定键映射到的值,如果是则返回 null该映射不包含键的映射。

语法

public V get(Object key)

这里,V 是容器维护的值的类型。

参数

key 指定要返回关联值的key。

返回值

返回指定键映射到的值,如果此映射不包含该键的映射,则返回 null。

异常

如果指定的键为 null,则抛出 NullPointerException

示例:

在下面的示例中,java.util .Hashtable.get() 方法返回给定哈希表中指定键映射到的值。

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //创建哈希表
    Hashtable<Integer, String> Htable = new Hashtable<Integer, String>();

    //填充哈希表
    Htable.put(101, "John");
    Htable.put(102, "Marry");
    Htable.put(103, "Kim");
    Htable.put(104, "Jo");

    //打印指定键的映射值
    System.out.println("101 is mapped to: " + Htable.get(101));
    System.out.println("102 is mapped to: " + Htable.get(102));
    System.out.println("103 is mapped to: " + Htable.get(103));
    System.out.println("104 is mapped to: " + Htable.get(104));  
  }
}

上述代码的输出将是:

101 is mapped to: John
102 is mapped to: Marry
103 is mapped to: Kim
104 is mapped to: Jo