Java.util.Dictionary.put() 方法
描述
java.util.Dictionary.put(K key,V value) 方法将指定的 key 映射到此字典中的指定 value。
声明
以下是 java.util.Dictionary.keys() 方法的声明
public abstract V put(K key,V value)
参数
key − 哈希表键。
value − 值。
返回值
此方法返回此字典中键映射到的值,如果键没有映射,则返回 null。
异常
NullPointerException − 如果 value 或 key 为 null
示例
下面的例子展示了 java.util.Dictionary.put() 方法的使用。
package com.tutorialspoint;
import java.util.*;
public class DictionaryDemo {
public static void main(String[] args) {
// create a new hasthtable
Dictionary d = new Hashtable();
// put some elements
d.put("1", "Chocolate");
d.put("2", "Cocoa");
d.put("5", "Coffee");
// print how many times put was called
System.out.println("Number of times put was called:" + d.size());
}
}
让我们编译并运行上面的程序,这将产生以下结果 −
Number of times put was called:3

