Java中的ConcurrentHashMap containsKey()方法

这个 JAVAutil。同时发生的ConcurrentHashMap。containsKey() 方法是Java中的一个内置函数,它接受一个参数并检查它是否是 地图

null

语法:

chm.containsKey(Object key_element)

参数: 该方法只接受一个参数 关键元素 要检查其是否为键的对象类型。

返回值: 如果指定了 关键元素 是一把钥匙 映射,否则为假。

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

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

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