如果指定对象是此哈希表中的键,则 java.util.Hashtable.containsKey() 方法返回 true。
语法
public boolean containsKey(Object key)
参数
key | 指定存在于其中的密钥 |
返回值
返回 true 指定对象是该哈希表中的键,否则返回 false .
异常
无。示例:
在下面的示例中,java.lang. util.Hashtable.containsKey() 方法用于检查给定哈希表中是否存在指定键。
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");
//检查103键是否存在
System.out.print("Does Htable contain 103 key? - ");
System.out.print(Htable.containsKey(103));
//检查105密钥是否存在
System.out.print("\nDoes Htable contain 105 key? - ");
System.out.print(Htable.containsKey(105));
}
}
上述代码的输出将是:
Does Htable contain 103 key? - true
Does Htable contain 105 key? - false