Java中的字典 get() 方法及示例
Dictionary 类的 get() 方法用于检索或获取参数中提到的特定键映射的值。当字典不包含键的此类映射时,它返回 NULL。
句法:
DICTIONARY.get(Object key_element)
参数:该方法采用对象类型的一个参数key_element ,并引用应该获取其关联值的键。
返回值:该方法返回与参数中的key_element关联的值。
下面的程序用于说明Java.util.Dictionary.get() 方法的工作:
方案一:
// Java code to illustrate the get() method
import java.util.*;
public class Dictionary_Demo {
public static void main(String[] args)
{
// Creating an empty Dictionary
Dictionary dict
= new Hashtable();
// Inserting the values into dictionary
dict.put(10, "Geeks");
dict.put(15, "4");
dict.put(20, "Geeks");
dict.put(25, "Welcomes");
dict.put(30, "You");
// Displaying the Dictionary
System.out.println("Initial Dictionary is: " + dict);
// Getting the value of 25
System.out.println("The Value is: " + dict.get(25));
// Getting the value of 10
System.out.println("The Value is: " + dict.get(10));
}
}
输出:
Initial Dictionary is: {10=Geeks, 20=Geeks, 30=You, 15=4, 25=Welcomes}
The Value is: Welcomes
The Value is: Geeks
方案二:
// Java code to illustrate the get() method
import java.util.*;
public class Dictionary_Demo {
public static void main(String[] args)
{
// Creating an empty Dictionary
Dictionary dict
= new Hashtable();
// Inserting the values into dictionary
dict.put("Geeks", 10);
dict.put("4", 15);
dict.put("Geeks", 20);
dict.put("Welcomes", 25);
dict.put("You", 30);
// Displaying the Dictionary
System.out.println("Initial Dictionary is: "
+ dict);
// Getting the value of 25
System.out.println("The Value is: "
+ dict.get("Geeks"));
// Getting the value of 10
System.out.println("The Value is: "
+ dict.get(20));
}
}
输出:
Initial Dictionary is: {You=30, Welcomes=25, 4=15, Geeks=20}
The Value is: 20
The Value is: null