AbstractMap.SimpleEntry <K,V>用於維護鍵和值條目。可以使用setValue方法更改該值。此類有助於構建自定義Map實現。
AbstractMap.SimpleEntry <K,V>的hashCode()方法返回此Map條目的哈希碼值。映射條目e的哈希碼可以通過以下方式計算:
(e.getKey()==null ? 0:e.getKey().hashCode()) ^ (e.getValue()==null ? 0:e.getValue().hashCode())
用法:
public int hashCode()
參數:此方法不接受任何內容。
返回值:此方法返回此映射條目的哈希碼值。
以下示例程序旨在說明hashCode()方法:
程序1:
// Java program to demonstrate
// AbstractMap.SimpleEntry.hashCode() method
import java.util.*;
import java.util.AbstractMap.SimpleEntry;
public class GFG {
@SuppressWarnings({ "unchecked", "rawtypes" })
public static void main(String[] args)
{
// create a ArrayList of Map
ArrayList<AbstractMap
.SimpleEntry<Integer, Integer> >
arrayList
= new ArrayList<AbstractMap
.SimpleEntry<Integer, Integer> >();
// add values
arrayList.add(new AbstractMap.SimpleEntry(0, 123));
arrayList.add(new AbstractMap.SimpleEntry(1, 130));
arrayList.add(new AbstractMap.SimpleEntry(2, 994));
// print keys
for (int i = 0; i < arrayList.size(); i++) {
// get map from list
AbstractMap.SimpleEntry<Integer, Integer>
map
= arrayList.get(i);
// get value from map using hashCode()
int value = map.hashCode();
System.out.println("HashCode = " + value);
}
}
}
輸出:
HashCode = 123 HashCode = 131 HashCode = 992
程序2:
// Java program to demonstrate
// AbstractMap.SimpleEntry.hashCode() method
import java.util.*;
public class GFG {
@SuppressWarnings({ "unchecked", "rawtypes" })
public static void main(String[] args)
{
// create a ArrayList of Map
ArrayList<AbstractMap
.SimpleEntry<String, String> >
arrayList
= new ArrayList<AbstractMap
.SimpleEntry<String, String> >();
// add values
arrayList.add(new AbstractMap
.SimpleEntry(" 001AB ", " Emp 1"));
arrayList.add(new AbstractMap
.SimpleEntry(" 011AC ", " Emp 2"));
arrayList.add(new AbstractMap
.SimpleEntry(" 111AD ", " Emp 3"));
arrayList.add(new AbstractMap
.SimpleEntry(" 101BE ", " Emp 4"));
arrayList.add(new AbstractMap
.SimpleEntry(" 110CE ", " Emp 5"));
// print keys
for (int i = 0; i < arrayList.size(); i++) {
// get map from list
AbstractMap.SimpleEntry<String, String>
map
= arrayList.get(i);
// get hashcode value from map using hashCode()
int value = map.hashCode();
System.out.println("hashCode = " + value);
}
}
}
輸出:
hashCode = -873386249 hashCode = -874293612 hashCode = -910934441 hashCode = -910552137 hashCode = -910906733
參考文獻: https://docs.oracle.com/javase/10/docs/api/java/util/AbstractMap.SimpleEntry.html#hashCode()
相關用法
- Java Map hashCode()用法及代碼示例
- Java Set hashCode()用法及代碼示例
- Java ZoneOffset hashCode()用法及代碼示例
- Java ChoiceFormat hashCode()用法及代碼示例
- Java ChronoZonedDateTime hashCode()用法及代碼示例
- Java Package hashCode()用法及代碼示例
- Java SimpleDateFormat hashCode()用法及代碼示例
- Java OptionalLong hashCode()用法及代碼示例
- Java AbstractSet hashCode()用法及代碼示例
- Java ChronoLocalDate hashCode()用法及代碼示例
- Java Duration hashCode()用法及代碼示例
- Java 8 Clock hashCode()用法及代碼示例
- Java ByteBuffer hashCode()用法及代碼示例
- Java ChronoLocalDateTime hashCode()用法及代碼示例
- Java Period hashCode()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 AbstractMap.SimpleEntry hashCode() Method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。