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<Integer, String> chm =
new ConcurrentHashMap<Integer, String>();
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<String, Integer> chm =
new ConcurrentHashMap<String, Integer>();
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://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentHashMap.html#containsKey()
相關用法
- Java LinkedHashMap containsKey()用法及代碼示例
- Java Map containsKey()用法及代碼示例
- Java EnumMap containsKey()用法及代碼示例
- Java WeakHashMap containsKey()用法及代碼示例
- Java IdentityHashMap containsKey()用法及代碼示例
- Java TreeMap containsKey()用法及代碼示例
- Java Hashtable containsKey()用法及代碼示例
- Java HashMap containsKey()用法及代碼示例
- Java Properties containsKey(value)用法及代碼示例
- Java ConcurrentSkipListMap containsKey()用法及代碼示例
- Java AbstractMap containsKey()用法及代碼示例
- Java SimpleBindings containsKey()用法及代碼示例
- Java ConcurrentHashMap get()用法及代碼示例
- Java ConcurrentHashMap put()用法及代碼示例
- Java ConcurrentHashMap size()用法及代碼示例
注:本文由純淨天空篩選整理自RICHIK BHATTACHARJEE大神的英文原創作品 ConcurrentHashMap containsKey() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。