Java中的ConcurrentSkipListMap.containsValue()方法及示例
java.util.concurrent.ConcurrentSkipListMap 中的 containsValue() 方法是Java中的一个内置函数,如果该映射将一个或多个键映射到指定的值,则返回 true 。如果没有要映射的键,则该方法返回null。当指定的值为null时,该方法会抛出 NullPointerException 。
语法:
public boolean containsValue(Object ob)
参数: 该函数接受单个强制参数 ob ,该参数指定要测试其存在的值。
返回值: 如果映射存在,则该函数返回真,否则返回假。
异常: 当指定的值为null时,该方法会抛出 NullPointerException 。
下面的程序说明了上述内容:
程序1:
// Java Program Demonstrate containsValue()
// method of ConcurrentSkipListMap
import java.util.concurrent.*;
class GFG {
public static void main(String[] args)
{
// Initializing the map
ConcurrentSkipListMap<Integer, Integer>
mpp = new ConcurrentSkipListMap<Integer,
Integer>();
// Adding elements to this map
for (int i = 1; i <= 5; i++)
mpp.put(i, i);
// checking whether object present in map
System.out.println(mpp.containsValue(4));
}
}
输出:
true
程序2:
// Java Program Demonstrate containsValue()
// method of ConcurrentSkipListMap
import java.util.concurrent.*;
class GFG {
public static void main(String[] args)
{
// Initializing the map
ConcurrentSkipListMap<Integer, Integer>
mpp = new ConcurrentSkipListMap<Integer,
Integer>();
// Adding elements to this map
for (int i = 1; i <= 5; i++)
mpp.put(i, i);
// checking whether object present in map
System.out.println(mpp.containsValue(7));
}
}
输出:
false
参考资料: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentSkipListMap.html#containsValue-java.lang.Object-
极客教程