Java中的ConcurrentSkipListMap.containsValue()方法及示例

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-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程