java.util.concurrent.ConcurrentHashMap的get()方法是Java中的內置函數,它接受鍵作為參數並返回映射到它的值。如果作為參數傳遞的鍵不存在映射,則返回null。
用法:
Concurrent.get(Object key_element)
參數:該方法接受對象類型的單個參數key_element,該參數表示應該返回其關聯值的鍵。
返回值:該方法返回與參數中的key_element關聯的值。
異常:當指定的key_element為null時,該函數將引發NullPointerException。
以下程序說明了java.util.concurrent.ConcurrentHashMap.get()方法的用法:
示例1:該程序涉及將字符串值映射到整數鍵。
// Java Program Demonstrate get()
// method of ConcurrentHashMap
import java.util.concurrent.*;
class GFG {
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, "Contribute");
// Displaying the HashMap
System.out.println("The Mappings are: ");
System.out.println(chm);
// Display the value of 100
System.out.println("The Value associated to "
+ "100 is : " + chm.get(100));
// Getting the value of 103
System.out.println("The Value associated to "
+ "103 is : " + chm.get(103));
}
}
輸出:
The Mappings are: {100=Geeks, 101=for, 102=Geeks, 103=Contribute} The Value associated to 100 is : Geeks The Value associated to 103 is : Contribute
示例2:該程序涉及將整數值映射到字符串鍵。
// Java Program Demonstrate get()
// method of ConcurrentHashMap
import java.util.concurrent.*;
class GFG {
public static void main(String[] args)
{
ConcurrentHashMap<String, Integer>
chm = new ConcurrentHashMap<String, Integer>();
chm.put("Geeks", 100);
chm.put("GFG", 10);
chm.put("GeeksforGeeks", 25);
chm.put("Contribute", 102);
// Displaying the HashMap
System.out.println("The Mappings are: ");
System.out.println(chm);
// Display the value of Geeks
System.out.println("The Value associated to "
+ "Geeks is : " + chm.get("Geeks"));
// Getting the value of Contribute
System.out.println("The Value associated to "
+ "Contribute is : " + chm.get("Contribute"));
}
}
輸出:
The Mappings are: {GeeksforGeeks=25, Geeks=100, GFG=10, Contribute=102} The Value associated to Geeks is : 100 The Value associated to Contribute is : 102
參考: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentHashMap.html#get()
相關用法
- Java ConcurrentHashMap put()用法及代碼示例
- Java ConcurrentHashMap isEmpty()用法及代碼示例
- Java ConcurrentHashMap size()用法及代碼示例
- Java ConcurrentHashMap remove()用法及代碼示例
- Java ConcurrentHashMap putIfAbsent()用法及代碼示例
- Java ConcurrentHashMap putAll()用法及代碼示例
- Java ConcurrentHashMap containsKey()用法及代碼示例
- Java ConcurrentHashMap containsValue()用法及代碼示例
- Java ConcurrentHashMap contains()用法及代碼示例
- Java ConcurrentHashMap compute()用法及代碼示例
- Java ConcurrentHashMap entrySet()用法及代碼示例
- Java ConcurrentHashMap computeIfAbsent()用法及代碼示例
- Java ConcurrentHashMap elements()用法及代碼示例
- Java ConcurrentHashMap values()用法及代碼示例
- Java ConcurrentHashMap keys()用法及代碼示例
注:本文由純淨天空篩選整理自RICHIK BHATTACHARJEE大神的英文原創作品 ConcurrentHashMap get() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。