📜  Java中的ConcurrentHashMap containsKey()方法

📅  最后修改于: 2022-05-13 01:54:52.182000             🧑  作者: Mango

Java中的ConcurrentHashMap containsKey()方法

Java .util.concurrent.ConcurrentHashMap.containsKey()方法是Java中的一个内置函数,它接受一个参数并检查它是否是这个映射中的一个键。

句法:

chm.containsKey(Object key_element)

参数:该方法接受对象类型的单个参数key_element ,以检查它是否是键。

返回值:如果指定的key_element映射的键,则该方法返回 true,否则返回 false。

异常:当指定的key_element为 null 时,函数会抛出 NullPointerException。

下面的程序说明了Java.util.concurrent.ConcurrentHashMap.containsKey()方法的使用:

程序 1:该程序涉及将字符串值映射到整数键。

/* Java Program Demonstrate containsKey()
   method of ConcurrentHashMap */
  
import java.util.concurrent.*;
class ConcurrentHashMapDemo {
    public static void main(String[] args)
    {
        ConcurrentHashMap chm =
                 new ConcurrentHashMap();
        chm.put(100, "Geeks");
        chm.put(101, "for");
        chm.put(102, "Geeks");
  
        // Checking whether 105 is a key of the map
        if (chm.containsKey(105)) {
            System.out.println("105 is a key.");
        }
        else {
            System.out.println("105 is not a key.");
        }
  
        // Checking whether 100 is a key of the map
        if (chm.containsKey(100)) {
            System.out.println("100 is a key.");
        }
        else {
            System.out.println("100 is not a key.");
        }
    }
}
输出:
105 is not a key.
100 is a key.

程序 2:该程序涉及将整数值映射到字符串键。

/* Java Program Demonstrate containsKey()
   method of ConcurrentHashMap */
  
import java.util.concurrent.*;
class ConcurrentHashMapDemo {
    public static void main(String[] args)
    {
        ConcurrentHashMap chm = 
                  new ConcurrentHashMap();
        chm.put("Geeks", 120);
        chm.put("for", 11);
        chm.put("GeeksforGeeks", 15);
        chm.put("Gfg", 50);
        chm.put("GFG", 25);
  
        // Checking whether GFG is a key of the map
        if (chm.containsKey("GFG")) {
            System.out.println("GFG is a key.");
        }
        else {
            System.out.println("GFG is not a key.");
        }
  
        // Checking whether Geek is a key of the map
        if (chm.containsKey("Geek")) {
            System.out.println("Geek is a key.");
        }
        else {
            System.out.println("Geek is not a key.");
        }
    }
}
输出:
GFG is a key.
Geek is not a key.

参考:https: Java/util/concurrent/ConcurrentHashMap.html#containsKey()