亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 編程 > Java > 正文

Java equals 方法與hashcode 方法的深入解析

2020-01-31 16:50:36
字體:
來源:轉載
供稿:網友

PS:本文使用jdk1.7
解析
1.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);
    }

看代碼,Object的equals方法,采用== 進行比較,只是比較對象的引用,如果引用的對象相同,那么就返回true.
看注釋,Object的equals方法,具有如下特性
1.reflexive-自反性 
 x.equals(x)  return true
2.symmetric-對稱性
x.equals(y)  return true
y.equals(x)  return true
3.transitive-傳遞性
x.equals(y)  return true
y.equals(z)  return true
x.equals(z)  return true
4.consistent-一致性
x.equals(y)  return true //那么不管調用多少次,肯定都是返回true
5.與null的比較
x.equals(null) return false //對于none-null的x對象,每次必然返回false
6.于hashcode的關系
     * 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.
需要注意的是,一般來說,如果重寫了equals方法,都必須要重寫hashcode方法,
來確保具有相同引用的對象,能夠具有同樣的hashcode值
好了,看到這里,我們就明白了,為什么重寫了equals方法,一般來說就需要重寫hashcode方法,
雖然這個不是強制性的,但是如果不能保證相同的引用對象,沒有相同的hashcode,會對系統留下很大隱患
2.String類的equals方法
復制代碼 代碼如下:

   /**
     * Compares this string to the specified object.  The result is {@code
     * true} if and only if the argument is not {@code null} and is a {@code
     * String} object that represents the same sequence of characters as this
     * object.
     *
     * @param  anObject
     *         The object to compare this {@code String} against
     *
     * @return  {@code true} if the given object represents a {@code String}
     *          equivalent to this string, {@code false} otherwise
     *
     * @see  #compareTo(String)
     * @see  #equalsIgnoreCase(String)
     */
    public boolean equals(Object anObject) {
        if (this == anObject) {
            return true;
        }
        if (anObject instanceof String) {
            String anotherString = (String) anObject;
            int n = value.length;
            if (n == anotherString.value.length) {
                char v1[] = value;
                char v2[] = anotherString.value;
                int i = 0;
                while (n-- != 0) {
                    if (v1[i] != v2[i])
                            return false;
                    i++;
                }
                return true;
            }
        }
        return false;
    }

看源碼,我們可以發現,這個比較分為兩部分
1.先比較是否引用同一對象
2.如果引用對象不同,是否兩個String的content相同
3,String 類的hashcode 方法
復制代碼 代碼如下:

    /**
     * Returns a hash code for this string. The hash code for a
     * <code>String</code> object is computed as
     * <blockquote><pre>
     * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
     * </pre></blockquote>
     * using <code>int</code> arithmetic, where <code>s[i]</code> is the
     * <i>i</i>th character of the string, <code>n</code> is the length of
     * the string, and <code>^</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;
    }

可以看到hashcode的計算公式為:s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
因此,對于同一個String,得出的hashcode必然是一致的
另外,對于空的字符串,hashcode的值是0

小結
至此,我們可以對本文開頭的疑問做一個小結.
1.字符串比較時用的什么方法,內部實現如何?
使用equals方法,先比較引用是否相同,后比較內容是否一致.

2.hashcode的作用,以及重寫equal方法,為什么要重寫hashcode方法?
hashcode是系統用來快速檢索對象而使用,equals方法是用來判斷引用的對象是否一致,所以,當引用對象一致時,必須要確保其hashcode也一致,因此需要重寫hashcode方法來確保這個一致性

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
国产精品一区电影| 亚洲丝袜av一区| 国内精品久久久久久中文字幕| 亚洲大胆人体在线| 91精品成人久久| 福利一区视频在线观看| 亚洲一区二区自拍| 国产成人97精品免费看片| 日韩精品极品视频| 亚洲美女免费精品视频在线观看| 精品国产91久久久| 在线播放日韩欧美| 国模吧一区二区三区| 国产精品久久久久久久久男| 亚洲欧美日韩一区二区三区在线| 欧美另类极品videosbest最新版本| 成人自拍性视频| 欧美国产日韩一区二区| 一区国产精品视频| 国产成人精品在线播放| 黑人巨大精品欧美一区二区一视频| 国产精品久久久久免费a∨| 日韩美女视频中文字幕| 久久91精品国产| 国内精品久久久久久| 亚洲免费福利视频| 免费99精品国产自在在线| 国产精品美女主播| 亚洲高清免费观看高清完整版| 国产日韩欧美日韩大片| 精品国产91久久久久久| 精品国产乱码久久久久久天美| 2019中文字幕在线免费观看| 欧美性猛交xxxx免费看漫画| 韩国国内大量揄拍精品视频| 国产91精品久久久久久| 国产精品成人品| 中文字幕亚洲第一| 久久久精品亚洲| 欧美国产日韩一区二区| 在线色欧美三级视频| 国产精品成人免费视频| 欧美中文在线免费| 亚洲精品乱码久久久久久按摩观| 亚洲成人免费网站| 成人免费淫片aa视频免费| 欧美乱妇高清无乱码| 亚洲a级在线观看| 久久精品视频99| 成人久久精品视频| 欧美日韩中文在线| 久久夜色精品国产| 欧美www视频在线观看| 日本久久中文字幕| 91国产精品91| 成人激情视频网| 久久久久免费视频| 亚洲欧美激情另类校园| 日韩av免费在线| 欧美成人手机在线| 色综合视频一区中文字幕| 欧美日韩免费在线观看| 亚洲精品国产综合区久久久久久久| 日产精品久久久一区二区福利| 亚洲国产精品va在线看黑人动漫| 国产成人精品久久久| 精品人伦一区二区三区蜜桃网站| 亚洲免费人成在线视频观看| 欧美在线观看网站| 日韩久久午夜影院| 国产69精品99久久久久久宅男| 97av在线视频| 亚洲国内精品视频| 日韩中文在线视频| 国产精品免费视频xxxx| 北条麻妃一区二区三区中文字幕| 欧美专区中文字幕| 91成人天堂久久成人| 久久在线观看视频| 欧美成人精品激情在线观看| 久久精品一区中文字幕| 亚洲欧美日韩一区二区在线| 中文字幕精品一区久久久久| 一区国产精品视频| 欧美另类在线播放| 国产欧美最新羞羞视频在线观看| 久久69精品久久久久久久电影好| 久久在精品线影院精品国产| 97**国产露脸精品国产| 久久久精品国产网站| 久热爱精品视频线路一| 91av在线不卡| 97精品国产91久久久久久| 26uuu久久噜噜噜噜| 国内精品中文字幕| 欧美一级淫片丝袜脚交| 亚洲国产精品高清久久久| 91中文在线观看| 免费97视频在线精品国自产拍| 欧美亚洲国产成人精品| 欧美色视频日本高清在线观看| 丰满岳妇乱一区二区三区| 国产精品一区二区三区在线播放| 亚洲精品一区二区在线| 奇米一区二区三区四区久久| 亚洲国产精品成人va在线观看| 啊v视频在线一区二区三区| 国产精品久久久久77777| 欧美成aaa人片在线观看蜜臀| 日韩欧美中文免费| 国产精品男人的天堂| www.欧美精品一二三区| 欧美日韩在线免费| 国产精品欧美一区二区| 国产精品美女在线| 欧美肥老太性生活视频| 日韩在线一区二区三区免费视频| 国产精品欧美日韩一区二区| 国产一区二区三区视频| 日日狠狠久久偷偷四色综合免费| 日韩欧美成人免费视频| 午夜精品久久久久久久久久久久| 日韩毛片在线观看| 日韩美女在线看| 国产精品入口日韩视频大尺度| 正在播放亚洲1区| 欧美精品在线视频观看| 国产精品老牛影院在线观看| 亚洲国产欧美久久| 日韩精品免费电影| 日韩成人在线网站| 亚洲第一福利视频| 欧美午夜丰满在线18影院| 亚洲高清久久网| 日韩av免费在线| 亚洲精品美女在线观看| 亚洲色无码播放| 8090理伦午夜在线电影| 日韩在线观看免费高清完整版| 国产不卡视频在线| 538国产精品视频一区二区| 国产97在线|亚洲| 国产做受高潮69| 亚洲欧美日韩国产中文| 国产裸体写真av一区二区| 欧美精品激情在线观看| 精品久久久久久久大神国产| 欧美精品午夜视频| 亚洲欧美另类在线观看| 久久久久日韩精品久久久男男| 国产精品9999| 国产一区二区在线免费视频| 日韩中文av在线| 国产91免费看片| 中文在线不卡视频| 深夜成人在线观看| 国产精品视频区1| 色999日韩欧美国产| 国语自产精品视频在线看抢先版图片| 国产精品啪视频| 国产一区二区三区在线免费观看| 亚洲天堂av在线播放| 精品亚洲一区二区三区在线播放| 色琪琪综合男人的天堂aⅴ视频|