java中怎么理解equals不相等,hashcode可能相等?

dd5407 2016-11-28 11:03:56
在HashMap中,重写equals之后还建议重写hashCode方法,可以减少链表形成的几率。如果hashCode不重写,hashCode在Object中就是和内存相关,那怎么还会出现hashcode相等,equals不相等的情况?求解~
...全文
2119 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
萧乡月夜 2016-12-01
  • 打赏
  • 举报
回复
引用 11 楼 dd5407 的回复:
[quote=引用 6 楼 aqzwss 的回复:] [quote=引用 5 楼 cmmchenmm 的回复:] 正好同问一下hashcode是指内存地址吗 如果是的话是不可能出现equal不同,hashcode不同的情况的,对象的数据值都不一样,那内存地址会一样吗?
hashcode就是返回一个int值,用于比较、存储。可以在自己的类中重写hashCode方法。[/quote] 这个int值是根据什么值来算的呢,与地址有关吗?[/quote] http://bbs.csdn.net/topics/320026340 里面有个10级的哥们把代码粘出来了 可以看看 虽然是1.6的
月明星稀灬 2016-12-01
  • 打赏
  • 举报
回复
eq默认是比较内存中存的对象,hashCode是指内存中的所存对象对应的KEY 你重写了eq方法,比较的就不一定是内存中的对象了啊。
萧乡月夜 2016-11-30
  • 打赏
  • 举报
回复
引用 11 楼 dd5407 的回复:
[quote=引用 6 楼 aqzwss 的回复:] [quote=引用 5 楼 cmmchenmm 的回复:] 正好同问一下hashcode是指内存地址吗 如果是的话是不可能出现equal不同,hashcode不同的情况的,对象的数据值都不一样,那内存地址会一样吗?
hashcode就是返回一个int值,用于比较、存储。可以在自己的类中重写hashCode方法。[/quote] 这个int值是根据什么值来算的呢,与地址有关吗?[/quote] 如果重写了hashcode,这个值的算法就由你自己实现。 比如你可以:

public int hashCode() {
        return 1;
    }
这样你的类每次计算hashcode的值都是1。 再比如String的hashCode:

    /**
     * Returns a hash code for this string. The hash code for a
     * {@code String} object is computed as
     * <blockquote><pre>
     * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
     * </pre></blockquote>
     * using {@code int} arithmetic, where {@code s[i]} is the
     * <i>i</i>th character of the string, {@code n} is the length of
     * the string, and {@code ^} indicates exponentiation.
     * (The hash value of the empty string is zero.)
     *
     * @return  a hash code value for this object.
     */
    public int hashCode() {
        int h = hash;
        if (h == 0 && value.length > 0) {
            char val[] = value;

            for (int i = 0; i < value.length; i++) {
                h = 31 * h + val[i];
            }
            hash = h;
        }
        return h;
    }
就是把每个字符从最高位开始乘以31再加上下一位。 以上是自己重写了hashCode方法,如果没有重写hashCode方法,则默认使用Object类中的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™ 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();
这是个native方法,也就是说会通过JNI调用本地方法,不同平台可能有不同实现。 如果想知道怎么算的,直接百度java Object类hashCode方法算法,一片一片的。 最后,说了这么多,应该值40分了吧
0萌萌哒0 2016-11-30
  • 打赏
  • 举报
回复
话说hashcode的默认实现返回的不是内存地址哦,只是一个“句柄”值。具体会不会相同呢,可能和虚拟机实现有关,我也不知道它是否保证不同的对象返回的值一定不同。。但是万一它对不同的对象返回了相同的值怎么办呢?so我们还是自己重写一下这个方法比较保险。 另,==比较两个引用是否指向同一个对象,equals根据用户自定义的规则判断两个对象是否相等(Object类的默认实现与==类似)
dd5407 2016-11-30
  • 打赏
  • 举报
回复
引用 6 楼 aqzwss 的回复:
[quote=引用 5 楼 cmmchenmm 的回复:] 正好同问一下hashcode是指内存地址吗 如果是的话是不可能出现equal不同,hashcode不同的情况的,对象的数据值都不一样,那内存地址会一样吗?
hashcode就是返回一个int值,用于比较、存储。可以在自己的类中重写hashCode方法。[/quote] 这个int值是根据什么值来算的呢,与地址有关吗?
萧乡月夜 2016-11-30
  • 打赏
  • 举报
回复
引用 9 楼 cmmchenmm 的回复:
[quote=引用 6 楼 aqzwss 的回复:] [quote=引用 5 楼 cmmchenmm 的回复:] 正好同问一下hashcode是指内存地址吗 如果是的话是不可能出现equal不同,hashcode不同的情况的,对象的数据值都不一样,那内存地址会一样吗?
hashcode就是返回一个int值,用于比较、存储。可以在自己的类中重写hashCode方法。[/quote] 要就是说hashcode不是内存地址了,那我就是之前没有理解到吧,那顺带问一下,有什么办法可以比较内存地址吗,是不是==? [/quote] 对。 ==比较地址 equals方法比较引用
NewshiJ 2016-11-30
  • 打赏
  • 举报
回复
引用 6 楼 aqzwss 的回复:
[quote=引用 5 楼 cmmchenmm 的回复:] 正好同问一下hashcode是指内存地址吗 如果是的话是不可能出现equal不同,hashcode不同的情况的,对象的数据值都不一样,那内存地址会一样吗?
hashcode就是返回一个int值,用于比较、存储。可以在自己的类中重写hashCode方法。[/quote] 要就是说hashcode不是内存地址了,那我就是之前没有理解到吧,那顺带问一下,有什么办法可以比较内存地址吗,是不是==?
NewshiJ 2016-11-30
  • 打赏
  • 举报
回复
引用 5 楼 cmmchenmm 的回复:
正好同问一下hashcode是指内存地址吗 如果是的话是不可能出现equal不同,hashcode不同的情况的,对象的数据值都不一样,那内存地址会一样吗?
要就是说hashcode不是内存地址了,那我就是之前没有理解到吧,那顺带问一下,有什么办法可以比较内存地址吗,是不是==?
heiguiDeng 2016-11-29
  • 打赏
  • 举报
回复
hash是通过一个方式转化成一组数字,有N分之一的机会会造成数字相同的巧合, 但是equal就需要一模一样,每一个字符每一个字节这个类的类型,参数
soton_dolphin 2016-11-29
  • 打赏
  • 举报
回复
很好理解,你和另一个人的体重(HASH CODE)可能一样,但是你们不是同一个人(EQUAL)
dd5407 2016-11-29
  • 打赏
  • 举报
回复
引用 1楼DarlieDouble 的回复:
一个是比较对象,一个是比较地址
对象不同不是地址不同吗
soton_dolphin 2016-11-29
  • 打赏
  • 举报
回复
比如说你用身份证判断equal, hashcode = 身份证每一位的数字的综合。 甲的身份证是 12345 乙的身份证是 54321 这样甲和乙的hashcode 都是以一样的 1 + 2+ 3+ 4+ 5 = 15 但是 12345 不equals 54321
萧乡月夜 2016-11-29
  • 打赏
  • 举报
回复
引用 5 楼 cmmchenmm 的回复:
正好同问一下hashcode是指内存地址吗 如果是的话是不可能出现equal不同,hashcode不同的情况的,对象的数据值都不一样,那内存地址会一样吗?
hashcode就是返回一个int值,用于比较、存储。可以在自己的类中重写hashCode方法。
NewshiJ 2016-11-29
  • 打赏
  • 举报
回复
正好同问一下hashcode是指内存地址吗 如果是的话是不可能出现equal不同,hashcode不同的情况的,对象的数据值都不一样,那内存地址会一样吗?
DarlieDouble 2016-11-29
  • 打赏
  • 举报
回复
一个是比较对象,一个是比较地址

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧