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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。