当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Field hashCode()用法及代码示例


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-



相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 Field hashCode() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。