[Java基础] | hashCode()

Object类的HashCode()

对于基类的hashCode()方法来说

    /**
     * Returns a hash code value for the object. This method is
     * supported for the benefit of hash tables such as those provided by
     * {@link java.util.HashMap}.
     * <p>
     * The general contract of {@code hashCode} is:
     * <ul>
     * <li>Whenever it is invoked on the same object more than once during
     *     an execution of a Java application, the {@code hashCode} method
     *     must consistently return the same integer, provided no information
     *     used in {@code equals} comparisons on the object is modified.
     *     This integer need not remain consistent from one execution of an
     *     application to another execution of the same application.
     * <li>If two objects are equal according to the {@code equals(Object)}
     *     method, then calling the {@code hashCode} method on each of
     *     the two objects must produce the same integer result.
     * <li>It is <em>not</em> required that if two objects are unequal
     *     according to the {@link java.lang.Object#equals(java.lang.Object)}
     *     method, then calling the {@code hashCode} method on each of the
     *     two objects must produce distinct integer results.  However, the
     *     programmer should be aware that producing distinct integer results
     *     for unequal objects may improve the performance of hash tables.
     * </ul>
     * <p>
     * As much as is reasonably practical, the hashCode method defined by
     * class {@code Object} does return distinct integers for distinct
     * objects. (This is typically implemented by converting the internal
     * address of the object into an integer, but this implementation
     * technique is not required by the
     * Java&trade; programming language.)
     *
     * @return  a hash code value for this object.
     * @see     java.lang.Object#equals(java.lang.Object)
     * @see     java.lang.System#identityHashCode
     */
    public native int hashCode();

可以看出来hashCode()是一个本地方法。对于注释稍微翻译一下就是

返回对象的哈希码值。支持此方法是为了便于使用 {@link java.util.HashMap} 提供的哈希表。
每当在 Java 应用程序执行期间对同一对象多次调用时,{@code hashCode} 方法必须始终返回相同的整数,前提是没有修改对象上 {@code equals} 比较中使用的信息。该整数不需要从应用程序的一次执行到同一应用程序的另一次执行保持一致。

  • 如果根据 {@code equals(Object)} 方法两个对象相等,则对两个对象中的每一个调用 {@code hashCode} 方法必须产生相同的整数结果。
  • 要求如果两个对象根据{@link java.lang.Objectequals(java.lang.Object)}方法不相等,则调用{@code hashCode}方法这两个对象中的每一个都必须产生不同的整数结果。但是,程序员应该意识到,为不相等的对象生成不同的整数结果可能会提高哈希表的性能。
    在合理可行的情况下,类 {@code Object} 定义的 hashCode 方法确实为不同的对象返回不同的整数。 (这通常通过将对象的内部地址转换为整数来实现,但 Java™ 编程语言不需要这种实现技术。)

hashCode()是干什么的?

  • 返回对象的哈希码值(int),便于支持HashMap中table[]的使用
  • equals()相同的两个对象,hashcode()一定相同。而equals()不相同的两个对象,hashcode()不一定相同。
  • 尽可能为不相等的对象生成不同的整数。

Object的equals()

   /**
     * Indicates whether some other object is "equal to" this one.
     * <p>
     * The {@code equals} method implements an equivalence relation
     * on non-null object references:
     * <ul>
     * <li>It is <i>reflexive</i>: for any non-null reference value
     *     {@code x}, {@code x.equals(x)} should return
     *     {@code true}.
     * <li>It is <i>symmetric</i>: for any non-null reference values
     *     {@code x} and {@code y}, {@code x.equals(y)}
     *     should return {@code true} if and only if
     *     {@code y.equals(x)} returns {@code true}.
     * <li>It is <i>transitive</i>: for any non-null reference values
     *     {@code x}, {@code y}, and {@code z}, if
     *     {@code x.equals(y)} returns {@code true} and
     *     {@code y.equals(z)} returns {@code true}, then
     *     {@code x.equals(z)} should return {@code true}.
     * <li>It is <i>consistent</i>: for any non-null reference values
     *     {@code x} and {@code y}, multiple invocations of
     *     {@code x.equals(y)} consistently return {@code true}
     *     or consistently return {@code false}, provided no
     *     information used in {@code equals} comparisons on the
     *     objects is modified.
     * <li>For any non-null reference value {@code x},
     *     {@code x.equals(null)} should return {@code false}.
     * </ul>
     * <p>
     * The {@code equals} method for class {@code Object} implements
     * the most discriminating possible equivalence relation on objects;
     * that is, for any non-null reference values {@code x} and
     * {@code y}, this method returns {@code true} if and only
     * if {@code x} and {@code y} refer to the same object
     * ({@code x == y} has the value {@code true}).
     * <p>
     * Note that it is generally necessary to override the {@code hashCode}
     * method whenever this method is overridden, so as to maintain the
     * general contract for the {@code hashCode} method, which states
     * that equal objects must have equal hash codes.
     *
     * @param   obj   the reference object with which to compare.
     * @return  {@code true} if this object is the same as the obj
     *          argument; {@code false} otherwise.
     * @see     #hashCode()
     * @see     java.util.HashMap
     */
    public boolean equals(Object obj) {
        return (this == obj);
    }

指示其他对象是否“等于”这个对象。

{@code equals} 方法实现等价关系
以下性质皆有非空约束
自反性。x.equals(x)
对称性。x.equals(y) === y.equals(x)
传递性。x.equals(y)&&y.equals(z) === x.equals(z)
一致性。对于非空元素x和y x.equals(x)的返回值相同
x.equals(null)返回false
{@code Object} 类的 {@code equals} 方法实现了对象上最有区别的可能等价关系;也就是说,对于任何非空引用值 {@code x} 和 {@code y},当且仅当 {@code x} 和 {@code y} 引用同一个对象时,此方法才返回 {@code true} ({@code x == y} 的值为 {@code true})。

请注意,只要 {@code hashCode} 方法被重写,一般都需要重写该方法,以维护 {@code hashCode} 方法的一般约定,即相等的对象必须具有相等的哈希码.

equals()是干什么的

主要是一种等于的思想。在基类中,equals()需要两个对象引用同一个对象。但是不同的类会对该方法有具体的实现,比如在String类中,equals()是将对象转换为char[],将所有字符都相等的两个对象定义为equals()

HashMap中的hashCode()

前文中提到了,hashCode()主要还是使用int值作为hash码,尽可能让不同的对象拥有不同的hashCode(),是为了防止碰撞.

 public final int hashCode() {
            return Objects.hashCode(key) ^ Objects.hashCode(value);
        }
为什么使用异或运算

异或运算是不进位的加法运算,相同为0不同为1。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值