Java.util.ConcurrentHashMap的contains()方法用於檢查某些鍵是否映射到該表中的指定值。它是執行特定任務的傳統方法。該操作類似於ConcurrentHashMap的containsValue()方法。
用法:
ConcurrentHashMap.contains(Object Value)
參數:該方法僅使用一個Object類型的參數Value,表示應該由映射內的任何鍵檢查其映射的值。
返回值:如果檢測到值的映射,則該方法返回布爾值true,否則返回false。
下麵的程序用於說明ConcurrentHashMap.containsValue()方法的用法:
示例1:將字符串值映射到整數鍵。
// Java code to illustrate the contains() method
import java.util.*;
import java.util.concurrent.*;
public class ConcurrentHashMapDemo {
public static void main(String[] args)
{
// Creating an empty HashMap
ConcurrentHashMap<Integer, String>
hash_map = new ConcurrentHashMap<Integer,
String>();
// Mapping string values to int keys
hash_map.put(10, "Geeks");
hash_map.put(15, "4");
hash_map.put(20, "Geeks");
hash_map.put(25, "Welcomes");
hash_map.put(30, "You");
// Displaying the ConcurrentHashMap
System.out.println("Initial Mappings are: "
+ hash_map);
// Checking for the Value 'Geeks'
System.out.println("Is the value 'Geeks' present? "
+ hash_map.containsValue("Geeks"));
// Checking for the Value 'World'
System.out.println("Is the value 'World' present? "
+ hash_map.containsValue("World"));
}
}
輸出:
Initial Mappings are: {20=Geeks, 25=Welcomes, 10=Geeks, 30=You, 15=4} Is the value 'Geeks' present? true Is the value 'World' present? false
示例2:將整數值映射到字符串鍵。
// Java code to illustrate the contains() method
import java.util.*;
import java.util.concurrent.*;
public class ConcurrentHashMapDemo {
public static void main(String[] args)
{
// Creating an empty ConcurrentHashMap
ConcurrentHashMap<String, Integer>
hash_map = new ConcurrentHashMap<String,
Integer>();
// Mapping int values to string keys
hash_map.put("Geeks", 10);
hash_map.put("4", 15);
hash_map.put("Geeks", 20);
hash_map.put("Welcomes", 25);
hash_map.put("You", 30);
// Displaying the ConcurrentHashMap
System.out.println("Initial Mappings are: "
+ hash_map);
// Checking for the Value 'Geeks'
System.out.println("Is the value 'Geeks' present? "
+ hash_map.contains(25));
// Checking for the Value 'World'
System.out.println("Is the value 'World' present? "
+ hash_map.contains(35));
}
}
輸出:
Initial Mappings are: {4=15, Geeks=20, You=30, Welcomes=25} Is the value 'Geeks' present? true Is the value 'World' present? false
注意:可以對具有不同數據類型的變化和組合的任何類型的映射執行相同的操作。
相關用法
- Java ConcurrentHashMap values()用法及代碼示例
- Java ConcurrentHashMap keySet()用法及代碼示例
- Java ConcurrentHashMap keys()用法及代碼示例
- Java ConcurrentHashMap elements()用法及代碼示例
- Java ConcurrentHashMap computeIfAbsent()用法及代碼示例
- Java ConcurrentHashMap compute()用法及代碼示例
- Java ConcurrentHashMap entrySet()用法及代碼示例
- Java ConcurrentHashMap put()用法及代碼示例
- Java ConcurrentHashMap get()用法及代碼示例
- Java ConcurrentHashMap remove()用法及代碼示例
- Java ConcurrentHashMap putAll()用法及代碼示例
- Java ConcurrentHashMap size()用法及代碼示例
- Java ConcurrentHashMap containsKey()用法及代碼示例
- Java ConcurrentHashMap isEmpty()用法及代碼示例
- Java ConcurrentHashMap putIfAbsent()用法及代碼示例
注:本文由純淨天空篩選整理自Chinmoy Lenka大神的英文原創作品 ConcurrentHashMap contains() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。