Java.util.Dictionary 类

java.util.Dictionary.get() 方法返回键在此字典中映射到的值。如果字典包含指定键的条目,则返回关联的值;

语法

public abstract V get(Object key)

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

参数

key 指定此字典中的键。

返回值

返回该字典中键映射到的值。如果键未映射到此字典中的任何值,则为 null。

Exception

如果键为 null,则抛出 NullPointerException

示例:

在下面的示例中,java.util.Dictionary.get() 方法返回键在给定字典。

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //创建字典
    Dictionary<Integer, String> Dict = 
        new Hashtable<Integer, String>();

    //填充字典
    Dict.put(101, "John");
    Dict.put(102, "Marry");
    Dict.put(103, "Kim");
    Dict.put(104, "Sam");
    Dict.put(105, "Jo");

    //打印字典的内容
    //使用get方法
    System.out.println("The value associated with 101 is: " + Dict.get(101));
    System.out.println("The value associated with 102 is: " + Dict.get(102));
    System.out.println("The value associated with 103 is: " + Dict.get(103));

  }
}

上述代码的输出将是:

The value associated with 101 is: John
The value associated with 102 is: Marry
The value associated with 103 is: Kim