ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Object--hashCode()

2022-08-03 09:32:04  阅读:154  来源: 互联网

标签:code java -- Object hashCode integer method


 hashCode()

 1 /**
 2      * Returns a hash code value for the object. This method is
 3      * supported for the benefit of hash tables such as those provided by
 4      * {@link java.util.HashMap}.
 5      * <p>
 6      * The general contract of {@code hashCode} is:
 7      * <ul>
 8      * <li>Whenever it is invoked on the same object more than once during
 9      *     an execution of a Java application, the {@code hashCode} method
10      *     must consistently return the same integer, provided no information
11      *     used in {@code equals} comparisons on the object is modified.
12      *     This integer need not remain consistent from one execution of an
13      *     application to another execution of the same application.
14      * <li>If two objects are equal according to the {@code equals(Object)}
15      *     method, then calling the {@code hashCode} method on each of
16      *     the two objects must produce the same integer result.
17      * <li>It is <em>not</em> required that if two objects are unequal
18      *     according to the {@link java.lang.Object#equals(java.lang.Object)}
19      *     method, then calling the {@code hashCode} method on each of the
20      *     two objects must produce distinct integer results.  However, the
21      *     programmer should be aware that producing distinct integer results
22      *     for unequal objects may improve the performance of hash tables.
23      * </ul>
24      * <p>
25      * As much as is reasonably practical, the hashCode method defined by
26      * class {@code Object} does return distinct integers for distinct
27      * objects. (This is typically implemented by converting the internal
28      * address of the object into an integer, but this implementation
29      * technique is not required by the
30      * Java&trade; programming language.)
31      *
32      * @return  a hash code value for this object.
33      * @see     java.lang.Object#equals(java.lang.Object)
34      * @see     java.lang.System#identityHashCode
35      */
36     public native int hashCode();
View Code

返回这个对象哈希码的值,支持这种方法是为了方便使用哈希表 --比如java.util.HashMap。

下面是hashCode约定的内容:

(1)在应用程序的执行期间,只要对象的equals方法的比较操作所用到的信息没有被修改,那么对同一个对象调用多次,hashCode方法都必须始终如一地返回同一个整数。在同一个应用程序的多次执行过程中,每次执行所返回的整数可以不一致。

(2)如果两个对象根据equals(Object)方法比较是相等的,那么在两个对象上分别调用hashCode()必须产生相同的整数结果。

(3)没有要求说如果两个对象根据equal()方法比较是不相等的,那么在两个对象上分别调用hashCode()产生的整数结果一定不相等。但是,程序员应该意识到,为不相等的对象产生不同的整数结果可以提高散列表的性能。

为了尽可能的实用,Object定义的hashCode方法,对于不同的对象返回的是不同的整数值(这通常是通过将对象的内部地址转换为整数来实现的,但是并非是由Java语言技术实现的)。

 

标签:code,java,--,Object,hashCode,integer,method
来源: https://www.cnblogs.com/liaowenhui/p/16545843.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有