java.lang.reflect.Field的hashCode()方法用於獲取此Field的哈希碼。最終哈希碼被計算為基礎字段聲明類名稱及其名稱的哈希碼的異或。如果對象不變,則哈希碼始終相同。哈希碼是由JVM在對象創建時生成的唯一代碼。它可用於對與哈希相關的算法(例如哈希表,哈希圖等)執行某些操作。也可以使用此唯一代碼搜索對象。
用法:
public int hashCode()
參數:此方法不接受任何內容。
返回值:此方法返回一個整數,該整數是此對象的哈希碼值。
以下示例程序旨在說明hashCode()方法:
程序1:
// Java program to demonstrate hashCode() method
import java.lang.reflect.Field;
public class GFG {
public static void main(String[] args)
throws Exception
{
// Get the marks field object
Field field = User.class.getField("Marks");
// Apply hashCode Method on User Object
// to get the hashCode of Marks field
int value = field.hashCode();
// print result
System.out.println("HashCode of Marks field"
+ " is " + value);
// Now Get the Fees field object
field = User.class.getField("Fees");
// Apply hashCode Method on User Object
// to get the hashcode of Fees field
value = field.hashCode();
// print result
System.out.println("HashCode of Fees field"
+ " is " + value);
// Now Get the name field object
field = User.class.getField("name");
// Apply hashCode Method on User Object
// to get the hashCode of name field
value = field.hashCode();
// print result
System.out.println("HashCode of name field"
+ " is " + value);
}
}
// sample User class
class User {
// static double values
public static double Marks = 34.13;
public static float Fees = 3413.99f;
public static String name = "Aman";
public static double getMarks()
{
return Marks;
}
public static void setMarks(double marks)
{
Marks = marks;
}
public static float getFees()
{
return Fees;
}
public static void setFees(float fees)
{
Fees = fees;
}
public static String getName()
{
return name;
}
public static void setName(String name)
{
User.name = name;
}
}
輸出:
HashCode of Marks field is 71482573 HashCode of Fees field is 591398 HashCode of name field is 1779040
程序2:
// Java program to demonstrate hashCode() method
import java.lang.reflect.Field;
import java.time.Month;
public class GFG {
public static void main(String[] args)
throws Exception
{
// Get all field objects of Month class
Field[] fields = Month.class.getFields();
for (int i = 0; i < fields.length; i++) {
// print name of Fields
System.out.println("HashCode of Field: "
+ fields[i].hashCode());
}
}
}
輸出:
HashCode of Field: -297508095 HashCode of Field: 1296412905 HashCode of Field: 1475695976 HashCode of Field: 1343692077 HashCode of Field: 1404020238 HashCode of Field: 1401709321 HashCode of Field: 1401709395 HashCode of Field: 538235208 HashCode of Field: 2125827066 HashCode of Field: -1718938229 HashCode of Field: -1007182215 HashCode of Field: 59532142
參考文獻: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#hashCode-
相關用法
- Java Field set()用法及代碼示例
- Java Field get()用法及代碼示例
- Java Map hashCode()用法及代碼示例
- Java Set hashCode()用法及代碼示例
- Java Field setChar()用法及代碼示例
- Java Field setFloat()用法及代碼示例
- Java Field getModifiers()用法及代碼示例
- Java Field setDouble()用法及代碼示例
- Java Field toGenericString()用法及代碼示例
- Java Field setShort()用法及代碼示例
- Java Field isSynthetic()用法及代碼示例
- Java Field setInt()用法及代碼示例
- Java Field getInt()用法及代碼示例
- Java Field getShort()用法及代碼示例
- Java Field getType()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 Field hashCode() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。