Dictionary get()方法在Java中的使用

Dictionary get()方法在Java中的使用

Dictionary类的get()方法用于检索或获取参数中指定的键映射的值。如果字典中不包含该键的映射,则返回NULL。

语法:

DICTIONARY.get(Object key_element)

参数: 该方法需要一个对象类型的参数key_element,它指的是要获取其相关值的键。

返回值: 方法返回与参数中的key_element相关联的值。

以下程序用于说明java.util.Dictionary.get()方法的工作方式:

程序1:

//Java代码演示get()方法
import java.util.*;
  
public class Dictionary_Demo {
    public static void main(String[] args)
    {
  
        // 创建一个空字典
        Dictionary dict
            = new Hashtable();
  
        // 将值插入到字典中
        dict.put(10, "Geeks");
        dict.put(15, "4");
        dict.put(20, "Geeks");
        dict.put(25, "Welcomes");
        dict.put(30, "You");
  
        // 显示字典
        System.out.println("初始字典:" + dict);
  
        // 获取25的值
        System.out.println("值为:" + dict.get(25));
  
        // 获取10的值
        System.out.println("值为:" + dict.get(10));
    }
}
初始字典:{10=Geeks, 20=Geeks, 30=You, 15=4, 25=Welcomes}
值为:Welcomes
值为:Geeks

程序2:

//Java代码演示get()方法
import java.util.*;
  
public class Dictionary_Demo {
    public static void main(String[] args)
    {
  
        // 创建一个空字典
        Dictionary dict
            = new Hashtable();
  
        // 将值插入到字典中
        dict.put("Geeks", 10);
        dict.put("4", 15);
        dict.put("Geeks", 20);
        dict.put("Welcomes", 25);
        dict.put("You", 30);
  
        // 显示字典
        System.out.println("初始字典:" + dict);
  
        // 获取"Geeks"的值
        System.out.println("值为:" + dict.get("Geeks"));
  
        // 获取20的值
        System.out.println("值为:" + dict.get(20));
    }
}
初始字典:{You=30, Welcomes=25, 4=15, Geeks=20}
值为:20
值为:null

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程