當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Method類 hashCode()用法及代碼示例


java.lang.reflect.Method.hashCode()方法返回Method類對象的哈希碼。返回的哈希碼是通過對方法的聲明類名和方法名的哈希碼進行異或運算而得出的。如果對象不變,則哈希碼始終相同。 Hashcode是由JVM在對象創建時生成的唯一代碼。它可用於對與哈希相關的算法(例如哈希表,哈希圖等)執行某些操作。也可以使用此唯一代碼搜索對象。

用法:

public int hashCode()

返回值:它返回一個整數值,該值表示此Method的hashCode值。



例:

Method:public void getvalue(){}
HashCode: 1553975225
Explanation:Hashcode is a unique code generated by the JVM at time of creation of the object
of Method getValue.when we going to apply hashCode function on method object of 
getValue it will return 1553975225 as hashCode.

Method:public void paint(){}
HashCode: 1643975341

以下示例程序旨在說明Method類的hashcode()方法:
程序1:獲取通過調用Class對象的getDeclaredMethod()創建的特定方法對象的哈希碼。

/* 
* Program Demonstrate hashcode() method of Method Class. 
*/
import java.lang.reflect.Method; 
  
public class GFG { 
  
    // create a Method name getSampleMethod 
    public void getSampleMethod() {} 
  
    // create main method 
    public static void main(String args[]) 
    { 
  
        try { 
  
            // create class object for class name GFG 
            Class c = GFG.class; 
  
            // get Method object of method name getSampleMethod 
            Method method = c.getDeclaredMethod("getSampleMethod", null); 
  
            // get hashcode of method object using hashCode() method 
            int hashCode = method.hashCode(); 
  
            // Print hashCode with method name 
            System.out.println("hashCode of method " + method.getName() 
                               + " is " + hashCode); 
        } 
        catch (Exception e) { 
            // print if any exception occures 
            e.printStackTrace(); 
        } 
    } 
}

輸出:

hashCode of method getSampleMethod is 1553813225

程序2:

在該程序中,通過調用類對象的getMethods()方法獲得類對象的Method對象的列表後,將為列表中的每個方法對象調用Method對象的hashCode()方法。最後,哈希碼與方法名稱一起打印。

/* 
* Program Demonstrate hashcode() method of Method Class. 
*/
import java.lang.reflect.Method; 
  
public class GFG { 
  
    // create a Method name getSampleMethod 
    public void getSampleMethod() {} 
  
    // create a Method name setSampleMethod 
    public String setSampleMethod() 
    { 
  
        String str = "hello India"; 
        return str; 
    } 
  
    // create main method 
    public static void main(String args[]) 
    { 
  
        try { 
  
            // create class object for class name GFG 
            Class c = GFG.class; 
  
            // get list of Method objects 
            // of class object of gfg class 
            Method[] methods = c.getMethods(); 
  
            // loop through methods list 
            // and get hashcode of every method 
            // and print those hashcode along with Method Name 
            for (Method m:methods) { 
  
                // get hashcode of current method of loop 
                int hashCode = m.hashCode(); 
  
                // Print hashCode along with method name 
                System.out.println("hashCode of method "
                                   + m.getName() 
                                   + " is " + hashCode); 
            } 
        } 
        catch (Exception e) { 
            // print Exception if any Exception occurs. 
            e.printStackTrace(); 
        } 
    } 
}

輸出:

hashCode of method main is 3282673
hashCode of method getSampleMethod is 1553813225
hashCode of method setSampleMethod is -1830532123
hashCode of method wait is 1063184614
hashCode of method wait is 1063184614
hashCode of method wait is 1063184614
hashCode of method equals is -1918826964
hashCode of method toString is -1451283457
hashCode of method hashCode is 933549448
hashCode of method getClass is 1261057617
hashCode of method notify is -43061542
hashCode of method notifyAll is 1312178187

說明:該程序的輸出還顯示除類對象中定義的方法(例如,wait,equals,toString,hashCode,getClass,notify和notifyAll)以外的方法對象的結果,這些方法是由類Object從java.lang包的超類對象繼承的。

參考:
https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#hashCode-




相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 Method Class | hashCode() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。