Java中Object的hashCode()方法

1.百度百科是这么定义的在这里插入图片描述
2.声明中是这么定义的
打开声明 int java.lang.Object.hashCode()

Returns a hash code value for the object. This method issupported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:
•Whenever it is invoked on the same object more than once duringan execution of a Java application, the hashCode methodmust consistently return the same integer, provided no informationused in equals comparisons on the object is modified.This integer need not remain consistent from one execution of anapplication to another execution of the same application.
•If two objects are equal according to the equals(Object)method, then calling the hashCode method on each ofthe two objects must produce the same integer result.
•It is not required that if two objects are unequalaccording to the java.lang.Object.equals(java.lang.Object)method, then calling the hashCode method on each of thetwo objects must produce distinct integer results. However, theprogrammer should be aware that producing distinct integer resultsfor unequal objects may improve the performance of hash tables.

As much as is reasonably practical, the hashCode method defined byclass Object does return distinct integers for distinctobjects. (This is typically implemented by converting the internaladdress of the object into an integer, but this implementationtechnique is not required by theJava™ programming language.)
返回:a hash code value for this object.另请参阅:java.lang.Object.equals(java.lang.Object)java.lang.System.identityHashCode
其实意思就是Returns a hash code value for the object. 返回对象的哈希代码值

3.作用是什么呢?
已知散列表(Hash table,也叫哈希表),是根据关键码值(Key value)而直接进行访问的数据结构。也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度。
那么所以hashcode()作用就是提高效率。
→→当向集合中插入对象时,如何判别在集合中是否已经存在该对象了?(注意:集合中不允许重复的元素存在)
也许大多数人都会想到调用equals方法来逐个进行比较,这个方法确实可行。但是如果集合中已经存在一万条数据或者更多的数据,如果采用equals方法去逐一比较,效率必然是一个问题。此时hashCode方法的作用就体现出来了,当集合要添加新的对象时,先调用这个对象的hashCode方法,得到对应的hashcode值,实际上在HashMap的具体实现中会用一个table保存已经存进去的对象的hashcode值,如果table中没有该hashcode值,它就可以直接存进去,不用再进行任何比较了;如果存在该hashcode值, 就调用它的equals方法与新元素进行比较,相同的话就不存了,不相同就散列其它的地址,所以这里存在一个冲突解决的问题,这样一来实际调用equals方法的次数就大大降低了,说通俗一点:Java中的hashCode方法就是根据一定的规则将与对象相关的信息(比如对象的存储地址,对象的字段等)映射成一个数值,这个数值称作为散列值。
下面是个测试

public class HashcodeTest {
	public static void main(String[] args) {
		/*
		 * Object类:
		 * 			类Object是类层次结构的根类,每个类都使用Object作为超类。
		 * 			每个类都直接或间接继承自Object类
		 * Object类的方法:
		 * 			public native int hashCode();返回该对象的hash码值
		 * 			注:哈希值是根据哈希算法算出来的一个值,这个值跟地址值有关,但不是实际地址值。
		 
		 * */
		Student s1 = new Student();
		System.out.println(s1.hashCode());
		System.out.println(s1);
		//366712642
		//javase2.Student@15db9742
		System.out.println("--------------------------");
		Student s2 = new Student();
		System.out.println(s2.hashCode());
		System.out.println(s2);
		//1829164700
		//javase2.Student@6d06d69c
		System.out.println("--------------------------");
		Student s3=s2;
		System.out.println(s3.hashCode());
		System.out.println(s3);
		//1829164700
		//javase2.Student@6d06d69c
	}
}

Java中Object的hashCode()方法_第1张图片

你可能感兴趣的:(java)