java.util.concurrent.ConcurrentHashMap.containsValue()方法是Java中的內置函數,該函數接受一個值,如果將一個或多個鍵映射到指定值,則返回true。此方法遍曆整個哈希表。因此,它的函數比containsKey()方法慢得多。
用法:
chm.containsValue(Object val_element)
參數:該方法接受對象類型的單個參數val_element,將檢查該對象是否已映射到映射中的任何鍵。
返回值:如果指定的val_element映射到此映射中的任何鍵,則該方法返回true,否則返回false。
異常:當指定的value_element為null時,該函數將引發NullPointerException。
以下程序說明了java.util.concurrent.ConcurrentHashMap.containsValue()方法的用法:
示例1:該程序涉及將整數值映射到字符串鍵。
/* Java Program to demonstrate containsValue()
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);
// Check whether a key is mapped to 100
if (chm.containsValue(100)) {
System.out.println("100 is mapped.");
}
else {
System.out.println("100 is not mapped.");
}
// Check whether a key is mapped to 120
if (chm.containsValue(120)) {
System.out.println("120 is mapped.");
}
else {
System.out.println("120 is not mapped.");
}
}
}
輸出:
100 is not mapped. 120 is mapped.
示例2:該程序涉及將字符串值映射到整數鍵。
/* Java Program to demonstrate containsValue()
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");
chm.put(103, "Gfg");
chm.put(104, "GFG");
// Check whether a key is mapped to Geeks
if (chm.containsValue("Geeks")) {
System.out.println("Geeks is mapped.");
}
else {
System.out.println("Geeks is not mapped.");
}
// Check whether a key is mapped to GfG
if (chm.containsValue("GfG")) {
System.out.println("GfG is mapped.");
}
else {
System.out.println("GfG is not mapped.");
}
}
}
輸出:
Geeks is mapped. GfG is not mapped.
相關用法
- Java Hashtable containsValue()用法及代碼示例
- Java WeakHashMap containsValue()用法及代碼示例
- Java HashMap containsValue()用法及代碼示例
- Java EnumMap containsValue(value)用法及代碼示例
- Java TreeMap containsValue()用法及代碼示例
- Java IdentityHashMap containsValue()用法及代碼示例
- Java Map containsValue()用法及代碼示例
- Java Properties containsValue(value)用法及代碼示例
- Java ConcurrentSkipListMap containsValue()用法及代碼示例
- Java AbstractMap containsValue()用法及代碼示例
- Java ConcurrentHashMap get()用法及代碼示例
- Java ConcurrentHashMap put()用法及代碼示例
- Java ConcurrentHashMap putAll()用法及代碼示例
- Java ConcurrentHashMap isEmpty()用法及代碼示例
- Java ConcurrentHashMap containsKey()用法及代碼示例
注:本文由純淨天空篩選整理自RICHIK BHATTACHARJEE大神的英文原創作品 ConcurrentHashMap containsValue() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。