📜  方法类 | Java中的hashCode()方法

📅  最后修改于: 2022-05-13 01:54:29.611000             🧑  作者: Mango

方法类 | Java中的hashCode()方法

Java.lang.reflect .Method.hashCode()方法返回 Method 类对象的哈希码。返回的哈希码是通过对方法的声明类名和方法名的哈希码进行异或运算来计算的。如果对象没有改变,哈希码总是相同的。 Hashcode 是 JVM 在创建对象时生成的唯一代码。它可用于对哈希表、哈希图等哈希相关算法执行一些操作。也可以使用此唯一代码搜索对象。

句法:

public int hashCode()

返回:它返回一个数值,表示此方法的 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() 方法:

方案一:通过调用Class对象的getDeclaredMethod()方法获取特定方法对象的hash码。

Java
/*
* 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 occurs
            e.printStackTrace();
        }
    }
}


Java
/*
* 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 getSampleMethod is 1553813225

方案二:
在这个程序中,通过调用类对象的getMethods()方法得到一个类对象的Method对象列表后,对该列表的每个方法对象调用Method对象的hashCode()方法。最后,哈希码与方法名称一起打印。

Java

/*
* 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 包的超类 Object 继承的。

参考:
https://docs.oracle.com/javase/8/docs/api/java Java