ConcurrentHashMap 類的 hashcode() 方法返回此 Map 的哈希碼值,即,對於映射中的每個鍵值對,key.hashCode()^value.hashCode() 的總和。
用法
public int hashCode()
參數
沒有傳遞參數。
返回
此Map的哈希碼值。
拋出
NullPointerException 。
例子1
//import statement
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
public class ConcurrentHashMaphashcodeExample1 {
public static void main(String args[]) {
ConcurrentHashMap<String , String> mymap = new ConcurrentHashMap<String ,String>();
ConcurrentHashMap<String , String> mymap2 = mymap;
mymap.put("AA", "Java");
mymap.put("BB", "angular");
mymap.put("CC", "JavaScript");
//print statements.
System.out.println("comparing mymap and mymap2:" +mymap.equals(mymap2));
System.out.println("comparing mymap and mymap2:" +mymap.hashCode());
}
}
輸出:
import java.util.concurrent.*; import java.util.*; public class ConcurrentHashMaphashcodeExample2 { public static void main(String[] args) { HashMap<String, Integer> mapcon = new HashMap<>(); mapcon.put("k1", 100); mapcon.put("k2", 200); mapcon.put("k3", 300); mapcon.put("k4", 400); System.out.println("HashMap values:\n " + mapcon.toString()); mapcon.computeIfAbsent("k5", k -> 200 + 300); mapcon.computeIfAbsent("k6", k -> 60 * 10); System.out.println("New HashMap after computeIfAbsent:\n "+ mapcon.hashCode()); } }
例子2
HashMap values:
{k1=100, k2=200, k3=300, k4=400}
New HashMap after computeIfAbsent:
20543
輸出:
相關用法
- Java ConcurrentHashMap contains()用法及代碼示例
- Java ConcurrentHashMap newKeySet()用法及代碼示例
- Java ConcurrentHashMap elements()用法及代碼示例
- Java ConcurrentHashMap forEach()用法及代碼示例
- Java ConcurrentHashMap putIfAbsent()用法及代碼示例
- Java ConcurrentHashMap computeIfAbsent()用法及代碼示例
- Java ConcurrentHashMap isEmpty()用法及代碼示例
- Java ConcurrentHashMap equals()用法及代碼示例
- Java ConcurrentHashMap putAll()用法及代碼示例
- Java ConcurrentHashMap mappingCount()用法及代碼示例
- Java ConcurrentHashMap merge()用法及代碼示例
- Java ConcurrentHashMap containsKey()用法及代碼示例
- Java ConcurrentHashMap put()用法及代碼示例
- Java ConcurrentHashMap computeIfPresent()用法及代碼示例
- Java ConcurrentHashMap toString()用法及代碼示例
- Java ConcurrentHashMap replace()用法及代碼示例
- Java ConcurrentHashMap clear()用法及代碼示例
- Java ConcurrentHashMap size()用法及代碼示例
- Java ConcurrentHashMap remove()用法及代碼示例
- Java ConcurrentHashMap keys()用法及代碼示例
注:本文由純淨天空篩選整理自 Java ConcurrentHashMap hashcode() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。