boolean containsKey(Object key)
描述 (Description)
containsKey(Object key)方法用于检查此映射是否包含指定键的映射。
声明 (Declaration)
以下是java.util.HashMap.containsKey()方法的声明。
public boolean containsKey(Object key)
参数 (Parameters)
key - 这是要在此地图中进行测试的密钥。
返回值 (Return Value)
如果此映射包含指定键的映射,则方法调用返回“true”。
异常 (Exception)
NA
例子 (Example)
以下示例显示了java.util.HashMap.containsKey()的用法
package com.iowiki;
import java.util.*;
public class HashMapDemo {
public static void main(String args[]) {
// create hash map
HashMap newmap = new HashMap();
// populate hash map
newmap.put(1, "tutorials");
newmap.put(2, "point");
newmap.put(3, "is best");
// check existence of key 2
System.out.println("Check if key 2 exists: " + newmap.containsKey(2));
}
}
让我们编译并运行上面的程序,这将产生以下结果。
Check if key 2 exists: true