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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。