ConcurrentHashMap在Java中包含value()方法

这个 JAVAutil。同时发生的ConcurrentHashMap。containsValue() 方法是Java中的内置函数,它接受一个值,如果一个或多个键映射到指定的值,则返回true。这个方法遍历整个哈希表。因此,它是一个比containsKey()方法慢得多的函数。

null

语法:

chm.containsValue(Object val_element)

参数: 该方法只接受一个参数 瓦卢元素 要检查其是否映射到映射中的任何键的对象类型。

返回值: 如果指定了 瓦卢元素 映射到中的任何键 映射,否则为假。

例外情况: 函数抛出 空指针异常 当指定 价值元素 是空的。

下面的程序说明了 JAVAutil。同时发生的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.

参考: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentHashMap.html#containsValue()

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享