美文网首页程序员
HashCode和Equals区别(一)

HashCode和Equals区别(一)

作者: RoundYuan | 来源:发表于2019-01-19 22:49 被阅读8次

我们首先来看一下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();

刨到这种地步,也差不多了,我们看一下上面的注释是啥意思?

返回对象的Hash Code值,支持此方法的好处是可以使用{@link java.util.HashMap}.提供的哈希表

每当在Java应用程序执行期间在同一个对象上多次调用该方法时,
{@code hashCode}方法必须一致地返回相同的整数,
前提是不修改对象上的{@code =}比较中使用的信息。
从应用程序的一次执行到同一应用程序的另一次执行,该整数不必保持一致。

String string = new String();
string="bbbb";
System.out.println(string.hashCode());
string="dddd";
System.out.println(string.hashCode());
HashCode结果1.png

如果根据{@code equals(Object)}方法两个对象相等,
那么在两个对象上调用{@code hashCode}方法必须产生相同的整数结果。

如果根据{@link java.lang.Object#equals(java.lang.Object)}方法两个对象不相等,
那么在两个对象上调用{@code hashCode}方法必须产生不同的整数结果,
这是不必要的。但是,程序员应该意识到,为不相等的对象生成不同的整数结果可能会提高哈希表的性能。

在相当实际的情况下,类{@code Object}定义的hashCode方法确实为不同的对象返回不同的整数。
(这通常是通过将对象的内部地址转换为整数来实现的,
但是Java&trade并不需要这种实现技术;编程语言)。

/**
     * 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);
    }
1、{@code =}方法在非空对象引用上实现等价关系:
2、它是自反的:对于任何非空引用值{@code x},
  {@code x.equals(x)}应该返回{@code true}。
3、它是对称的:对于任何非空的引用值{@code x}和{@code y},
  当且仅当{@code y = (x)}返回{@code true}时,
  {@code x.equals(y)}应该返回{@code true}。
  


4、 它是传递的:对于任何非空的参考值{@code x}, {@code y}, {@code z},如果{@code x.equals(y)}返回{@code true},
{@code y.equals(z)}返回{@code true},那么{@code x.equals(z)}应该返回{@code true}。

5、它是一致的:对于任何非空引用值{@code x}和{@code y},对{@code x.equals(y)}的多次调用
一致返回{@code true}或一致返回{@code false},但在对象被修改。
6、对于任何非空引用值{@code x}, {@code x.equals(null)}应该返回{@code false}。

7、{@code eauals}方法类{@codeObject}实现最歧视可能等价关系对象,也就是说,对于任何非空引用值{@code x}和
{@code y},该方法返回{@code true}当且仅当{@code x}和{@code y}引用同一个对象({@code x = = y}
{@code true})的值。

8、注意,通常需要在重写该方法时重写{@code hashCode}方法,以便维护{@code hashCode}方法的通用契约,
该契约声明相等的对象必须具有相等的散列代码。

相关文章

  • HashCode和Equals的区别

    HashCode和equals的区别: String方法的equals方法和HashCode方法 String的H...

  • Java 比较相等

    Java Equals() 特性 Equals() 和 == 的区别 重写Equals方法 重写HashCode方法

  • Java 拆箱与装箱

    本文知识点 基本类型与引用类型 == 与 equals() 的区别 equals() 和 hashCode 的关系...

  • equals和hashcode区别?

    equals()和hashCode()区别? equals():反映的是对象或变量具体的值,即两个对象里面包含的值...

  • HashCode和Equals区别(一)

    我们首先来看一下HashCode的源码: 刨到这种地步,也差不多了,我们看一下上面的注释是啥意思? 返回对象的Ha...

  • Android java 高级面试题库

    (一) java基础面试知识点 1,java中==和equals和hashCode的区别? 1、“==” =...

  • equals()和hashCode()面试考点

    1 问题的提出2 equals()和hashCode()区别3 为什么选择hashcode方法4 为什么要重写eq...

  • 2020-08-29

    java基础 一、java 中==和 equals 和 hashCode 的区别 1、对象类型不同 1、equal...

  • 为什么重写equals一定要重写hashCode

    一.Object类中的equals和hashCode 众所周知,equals和hashCode是java.lang...

  • Java基础面试题

    JDK 和 JRE 有什么区别? == 和 equals 的区别是什么? 两个对象的 hashCode() 相同,...

网友评论

    本文标题:HashCode和Equals区别(一)

    本文链接:https://www.haomeiwen.com/subject/eedtjqtx.html