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

这个 JAVA朗,反思 方法hashCode() 方法返回方法类对象的哈希代码。返回的hashcode是通过对该方法的声明类名和方法名的hashcodes执行异或操作来计算的。如果对象不变,哈希代码总是相同的。Hashcode是JVM在创建对象时生成的唯一代码。它可以用于对哈希表、哈希映射等哈希相关算法执行某些操作。还可以使用此唯一代码搜索对象。

null

语法:

public int hashCode()

返回: 它返回一个 整数 值,该值表示此方法的哈希代码值。

例子:

Method: public void getvalue(){}HashCode: 1553975225Explanation: Hashcode is a unique code generated by the JVM at time of creation of the objectof 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: 获取通过调用类对象的getDeclaredMethod()创建的特定方法对象的哈希代码。

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();
}
}
}


输出:

hashCode of method getSampleMethod is 1553813225

项目2: 在这个程序中,通过调用class object的getMethods()方法获得class object的Method对象列表后,会为列表中的每个Method对象调用Method object的hashCode()方法。最后,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 3282673hashCode of method getSampleMethod is 1553813225hashCode of method setSampleMethod is -1830532123hashCode of method wait is 1063184614hashCode of method wait is 1063184614hashCode of method wait is 1063184614hashCode of method equals is -1918826964hashCode of method toString is -1451283457hashCode of method hashCode is 933549448hashCode of method getClass is 1261057617hashCode of method notify is -43061542hashCode of method notifyAll is 1312178187

说明: 这个程序的输出还显示了方法对象的结果,而不是类对象中定义的方法,比如wait、equals、toString、hashCode、getClass、notify和notifyAll,它们是从java的超类对象继承的。lang按类对象打包。

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

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享