hashCode() 鍜宔quals() 鍖哄埆鍜屼綔鐢紙杞級

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hashCode() 鍜宔quals() 鍖哄埆鍜屼綔鐢紙杞級相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/%e8%ae%a1%e7%ae%97' title='璁$畻'>璁$畻   鍋囪   .com   stat   鏂板缓   table   鍏跺畠   ==   杩斿洖   

鍑哄锛歨ttps://www.jianshu.com/p/5a7f5f786b75

鏈珷鐨勫唴瀹逛富瑕佽В鍐充笅闈㈠嚑涓棶棰橈細

1 equals() 鐨勪綔鐢ㄦ槸浠€涔堬紵
2 equals() 涓?== 鐨勫尯鍒槸浠€涔堬紵
3 hashCode() 鐨勪綔鐢ㄦ槸浠€涔堬紵
4 hashCode() 鍜?equals() 涔嬮棿鏈変粈涔堣仈绯伙紵

绗?閮ㄥ垎 equals() 鐨勪綔鐢?/h1>

equals() 鐨勪綔鐢ㄦ槸 鐢ㄦ潵鍒ゆ柇涓や釜瀵硅薄鏄惁鐩哥瓑銆?/p>

equals() 瀹氫箟鍦↗DK鐨凮bject.java涓€傞€氳繃鍒ゆ柇涓や釜瀵硅薄鐨勫湴鍧€鏄惁鐩哥瓑(鍗筹紝鏄惁鏄悓涓€涓璞?鏉ュ尯鍒嗗畠浠槸鍚︾浉绛夈€傛簮鐮佸涓嬶細

public boolean equals(Object obj) {
    return (this == obj);
}

鏃㈢劧Object.java涓畾涔変簡equals()鏂规硶锛岃繖灏辨剰鍛崇潃鎵€鏈夌殑Java绫婚兘瀹炵幇浜唀quals()鏂规硶锛屾墍鏈夌殑绫婚兘鍙互閫氳繃equals()鍘绘瘮杈冧袱涓璞℃槸鍚︾浉绛夈€?浣嗘槸锛屾垜浠凡缁忚杩囷紝浣跨敤榛樿鐨?ldquo;equals()”鏂规硶锛岀瓑浠蜂簬“==”鏂规硶銆傚洜姝わ紝鎴戜滑閫氬父浼氶噸鍐檈quals()鏂规硶锛氳嫢涓や釜瀵硅薄鐨勫唴瀹圭浉绛夛紝鍒檈quals()鏂规硶杩斿洖true锛涘惁鍒欙紝杩斿洖fasle銆?/p>

涓嬮潰鏍规嵁“绫绘槸鍚﹁鐩杄quals()鏂规硶”锛屽皢瀹冨垎涓?绫汇€?br /> (01) 鑻ユ煇涓被娌℃湁瑕嗙洊equals()鏂规硶锛屽綋瀹冪殑閫氳繃equals()姣旇緝涓や釜瀵硅薄鏃讹紝瀹為檯涓婃槸姣旇緝涓や釜瀵硅薄鏄笉鏄悓涓€涓璞°€傝繖鏃讹紝绛変环浜庨€氳繃“==”鍘绘瘮杈冭繖涓や釜瀵硅薄銆?br /> (02) 鎴戜滑鍙互瑕嗙洊绫荤殑equals()鏂规硶锛屾潵璁〆quals()閫氳繃鍏跺畠鏂瑰紡姣旇緝涓や釜瀵硅薄鏄惁鐩哥瓑銆傞€氬父鐨勫仛娉曟槸锛氳嫢涓や釜瀵硅薄鐨勫唴瀹圭浉绛夛紝鍒檈quals()鏂规硶杩斿洖true锛涘惁鍒欙紝杩斿洖fasle銆?/p>

涓嬮潰锛屼妇渚嬪涓婇潰鐨?绉嶆儏鍐佃繘琛岃鏄庛€?/p>

  1. “娌℃湁瑕嗙洊equals()鏂规硶”鐨勬儏鍐?/li>

浠g爜濡備笅 (EqualsTest1.java)锛?/p>

import java.util.*;
import java.lang.Comparable;

/**
 * @desc equals()鐨勬祴璇曠▼搴忋€?
 *
 * @author skywang
 * @emai [email protected]
 */
public class EqualsTest1{

    public static void main(String[] args) {
        // 鏂板缓2涓浉鍚屽唴瀹圭殑Person瀵硅薄锛?
        // 鍐嶇敤equals姣旇緝瀹冧滑鏄惁鐩哥瓑
        Person p1 = new Person("eee", 100);
        Person p2 = new Person("eee", 100);
        System.out.printf("%s
", p1.equals(p2));
    }

    /**
     * @desc Person绫汇€?
     */
    private static class Person {
        int age;
        String name;

        public Person(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public String toString() {
            return name + " - " +age;
        }
    }
}
杩愯缁撴灉锛?span class="hljs-literal">false

缁撴灉鍒嗘瀽锛?/p>

   鎴戜滑閫氳繃 p1.equals(p2) 鏉?ldquo;姣旇緝p1鍜宲2鏄惁鐩哥瓑鏃?rdquo;銆傚疄闄呬笂锛岃皟鐢ㄧ殑Object.java鐨別quals()鏂规硶锛屽嵆璋冪敤鐨?(p1==p2) 銆傚畠鏄瘮杈?ldquo;p1鍜宲2鏄惁鏄悓涓€涓璞?rdquo;銆?
   鑰岀敱 p1 鍜?p2 鐨勫畾涔夊彲鐭ワ紝瀹冧滑铏界劧鍐呭鐩稿悓锛涗絾瀹冧滑鏄袱涓笉鍚岀殑瀵硅薄锛佸洜姝わ紝杩斿洖缁撴灉鏄?span class="hljs-literal">false銆?
  1. "瑕嗙洊equals()鏂规硶"鐨勬儏鍐?/li>

鎴戜滑淇敼涓婇潰鐨凟qualsTest1.java锛氳鐩杄quals()鏂规硶銆?/p>

浠g爜濡備笅 (EqualsTest2.java)锛?/p>

import java.util.*;
import java.lang.Comparable;

/**
 * @desc equals()鐨勬祴璇曠▼搴忋€?
 * 
 */
public class EqualsTest2 {

    public static void main(String[] args) {
        // 鏂板缓2涓浉鍚屽唴瀹圭殑Person瀵硅薄锛?
        // 鍐嶇敤equals姣旇緝瀹冧滑鏄惁鐩哥瓑
        Person p1 = new Person("eee", 100);
        Person p2 = new Person("eee", 100);
        System.out.printf("%s
", p1.equals(p2));
    }

    /**
     * @desc Person绫汇€?
     */
    private static class Person {
        int age;
        String name;

        public Person(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public String toString() {
            return name + " - " + age;
        }

        /**
         * @desc 瑕嗙洊equals鏂规硶
         */
        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }

            // 濡傛灉鏄悓涓€涓璞¤繑鍥瀟rue锛屽弽涔嬭繑鍥瀎alse
            if (this == obj) {
                return true;
            }

            // 鍒ゆ柇鏄惁绫诲瀷鐩稿悓
            if (this.getClass() != obj.getClass()) {
                return false;
            }

            Person person = (Person) obj;
            return name.equals(person.name) && age == person.age;
        }
    }
}
杩愯缁撴灉锛?span class="hljs-literal">true

缁撴灉鍒嗘瀽锛?/p>

鎴戜滑鍦‥qualsTest2.java 涓噸鍐欎簡Person鐨別quals()鍑芥暟锛氬綋涓や釜Person瀵硅薄鐨?name 鍜?age 閮界浉绛夛紝鍒欒繑鍥瀟rue銆?br /> 鍥犳锛岃繍琛岀粨鏋滆繑鍥瀟rue銆?/p>

璁插埌杩欓噷锛岄『渚胯涓€涓媕ava瀵筫quals()鐨勮姹傘€傛湁浠ヤ笅鍑犵偣锛?/p>

1. 瀵圭О鎬э細濡傛灉x.equals(y)杩斿洖鏄?span class="hljs-string">"true"锛岄偅涔坹.equals(x)涔熷簲璇ヨ繑鍥炴槸"true"銆?
2. 鍙嶅皠鎬э細x.equals(x)蹇呴』杩斿洖鏄?span class="hljs-string">"true"銆?
3. 绫绘帹鎬э細濡傛灉x.equals(y)杩斿洖鏄?span class="hljs-string">"true"锛岃€屼笖y.equals(z)杩斿洖鏄?span class="hljs-string">"true"锛岄偅涔坺.equals(x)涔熷簲璇ヨ繑鍥炴槸"true"銆?
4. 涓€鑷存€э細濡傛灉x.equals(y)杩斿洖鏄?span class="hljs-string">"true"锛屽彧瑕亁鍜寉鍐呭涓€鐩翠笉鍙橈紝涓嶇浣犻噸澶峹.equals(y)澶氬皯娆★紝杩斿洖閮芥槸"true"銆?
5. 闈炵┖鎬э紝x.equals(null)锛屾案杩滆繑鍥炴槸"false"锛泋.equals(鍜寈涓嶅悓绫诲瀷鐨勫璞?姘歌繙杩斿洖鏄?span class="hljs-string">"false"銆?

鐜板湪锛屽啀鍥為【涓€涓媏quals()鐨勪綔鐢細鍒ゆ柇涓や釜瀵硅薄鏄惁鐩哥瓑銆傚綋鎴戜滑閲嶅啓equals()鐨勬椂鍊欙紝鍙崈涓囦笉濂藉皢瀹冪殑浣滅敤缁欐敼鍙樹簡锛?/p>

绗?閮ㄥ垎 equals() 涓?== 鐨勫尯鍒槸浠€涔堬紵

== : 瀹冪殑浣滅敤鏄垽鏂袱涓璞$殑鍦板潃鏄笉鏄浉绛夈€傚嵆锛屽垽鏂袱涓璞℃槸涓嶈瘯鍚屼竴涓璞°€?/p>

equals() : 瀹冪殑浣滅敤涔熸槸鍒ゆ柇涓や釜瀵硅薄鏄惁鐩哥瓑銆備絾瀹冧竴鑸湁涓ょ浣跨敤鎯呭喌(鍓嶉潰绗?閮ㄥ垎宸茶缁嗕粙缁嶈繃)锛?br /> 鎯呭喌1锛岀被娌℃湁瑕嗙洊equals()鏂规硶銆傚垯閫氳繃equals()姣旇緝璇ョ被鐨勪袱涓璞℃椂锛岀瓑浠蜂簬閫氳繃“==”姣旇緝杩欎袱涓璞°€?br /> 鎯呭喌2锛岀被瑕嗙洊浜唀quals()鏂规硶銆備竴鑸紝鎴戜滑閮借鐩杄quals()鏂规硶鏉ヤ袱涓璞$殑鍐呭鐩哥瓑锛涜嫢瀹冧滑鐨勫唴瀹圭浉绛夛紝鍒欒繑鍥瀟rue(鍗筹紝璁や负杩欎袱涓璞$浉绛?銆?/p>

涓嬮潰锛岄€氳繃绀轰緥姣旇緝瀹冧滑鐨勫尯鍒€?/p>

浠g爜濡備笅锛?/p>

import java.util.*;
import java.lang.Comparable;

/**
 * @desc equals()鐨勬祴璇曠▼搴忋€?
 *
 * @author skywang
 * @emai [email protected]
 */
public class EqualsTest3{

    public static void main(String[] args) {
        // 鏂板缓2涓浉鍚屽唴瀹圭殑Person瀵硅薄锛?
        // 鍐嶇敤equals姣旇緝瀹冧滑鏄惁鐩哥瓑
        Person p1 = new Person("eee", 100);
        Person p2 = new Person("eee", 100);
        System.out.printf("p1.equals(p2) : %s
", p1.equals(p2));
        System.out.printf("p1==p2 : %s
", p1==p2);
    }

    /**
     * @desc Person绫汇€?
     */
    private static class Person {
        int age;
        String name;

        public Person(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public String toString() {
            return name + " - " +age;
        }

        /** 
         * @desc 瑕嗙洊equals鏂规硶 
         */  
        @Override
        public boolean equals(Object obj){  
            if(obj == null){  
                return false;  
            }  
              
            //濡傛灉鏄悓涓€涓璞¤繑鍥瀟rue锛屽弽涔嬭繑鍥瀎alse  
            if(this == obj){  
                return true;  
            }  
              
            //鍒ゆ柇鏄惁绫诲瀷鐩稿悓  
            if(this.getClass() != obj.getClass()){  
                return false;  
            }  
              
            Person person = (Person)obj;  
            return name.equals(person.name) && age==person.age;  
        } 
    }
}
  杩愯缁撴灉锛?
  p1.equals(p2) : true
  p1==p2 : false

缁撴灉鍒嗘瀽锛?/p>

鍦‥qualsTest3.java 涓細
(01) p1.equals(p2)
杩欐槸鍒ゆ柇p1鍜宲2鐨勫唴瀹规槸鍚︾浉绛夈€傚洜涓篜erson瑕嗙洊equals()鏂规硶锛岃€岃繖涓猠quals()鏄敤鏉ュ垽鏂璸1鍜宲2鐨勫唴瀹规槸鍚︾浉绛夛紝鎭版伆p1鍜宲2鐨勫唴瀹瑰張鐩哥瓑锛涘洜姝わ紝杩斿洖true銆?/p>

(02) p1==p2
杩欐槸鍒ゆ柇p1鍜宲2鏄惁鏄悓涓€涓璞°€傜敱浜庡畠浠槸鍚勮嚜鏂板缓鐨勪袱涓狿erson瀵硅薄锛涘洜姝わ紝杩斿洖false銆?/p>

绗?閮ㄥ垎 hashCode() 鐨勪綔鐢?/h1>

hashCode() 鐨勪綔鐢ㄦ槸鑾峰彇鍝堝笇鐮侊紝涔熺О涓烘暎鍒楃爜锛涘畠瀹為檯涓婃槸杩斿洖涓€涓猧nt鏁存暟銆傝繖涓搱甯岀爜鐨勪綔鐢ㄦ槸纭畾璇ュ璞″湪鍝堝笇琛ㄤ腑鐨勭储寮曚綅缃€?/p>

hashCode() 瀹氫箟鍦↗DK鐨凮bject.java涓紝杩欏氨鎰忓懗鐫€Java涓殑浠讳綍绫婚兘鍖呭惈鏈塰ashCode() 鍑芥暟銆?br /> 铏界劧锛屾瘡涓狫ava绫婚兘鍖呭惈hashCode() 鍑芥暟銆備絾鏄紝浠呬粎褰撳垱寤哄苟鏌愪釜“绫荤殑鏁e垪琛?rdquo;(鍏充簬“鏁e垪琛?rdquo;瑙佷笅闈㈣鏄?鏃讹紝璇ョ被鐨刪ashCode() 鎵嶆湁鐢?浣滅敤鏄細纭畾璇ョ被鐨勬瘡涓€涓璞″湪鏁e垪琛ㄤ腑鐨勪綅缃紱鍏跺畠鎯呭喌涓?渚嬪锛屽垱寤虹被鐨勫崟涓璞★紝鎴栬€呭垱寤虹被鐨勫璞℃暟缁勭瓑绛?锛岀被鐨刪ashCode() 娌℃湁浣滅敤銆?br /> 涓婇潰鐨勬暎鍒楄〃锛屾寚鐨勬槸锛欽ava闆嗗悎涓湰璐ㄦ槸鏁e垪琛ㄧ殑绫伙紝濡侶ashMap锛孒ashtable锛孒ashSet銆?/p>

   涔熷氨鏄锛歨ashCode() 鍦ㄦ暎鍒楄〃涓墠鏈夌敤锛屽湪鍏跺畠鎯呭喌涓嬫病鐢ㄣ€傚湪鏁e垪琛ㄤ腑hashCode() 鐨勪綔鐢ㄦ槸鑾峰彇瀵硅薄鐨勬暎鍒楃爜锛岃繘鑰岀‘瀹氳瀵硅薄鍦ㄦ暎鍒楄〃涓殑浣嶇疆銆?

OK锛佽嚦姝わ紝鎴戜滑鎼炴竻妤氫簡锛歨ashCode()鐨勪綔鐢ㄦ槸鑾峰彇鏁e垪鐮併€備絾鏄紝鏁e垪鐮佹槸鐢ㄦ潵骞蹭粈涔堢殑鍛紵涓轰粈涔堟暎鍒楄〃闇€瑕佹暎鍒楃爜鍛紵瑕佽В鍐宠繖浜涢棶棰橈紝灏遍渶瑕佺悊瑙f暎鍒楄〃锛佸叧浜庢暎鍒楄〃鐨勫唴瀹癸紝闈炰笁瑷€涓よ閬撶殑鏄庣櫧锛?/p>

涓轰簡鑳界悊瑙e悗闈㈢殑鍐呭锛岃繖閲岀畝鍗曠殑浠嬬粛涓€涓嬫暎鍒楃爜鐨勪綔鐢ㄣ€?/p>

鎴戜滑閮界煡閬擄紝鏁e垪琛ㄥ瓨鍌ㄧ殑鏄敭鍊煎(key-value)锛屽畠鐨勭壒鐐规槸锛氳兘鏍规嵁“閿?rdquo;蹇€熺殑妫€绱㈠嚭瀵瑰簲鐨?ldquo;鍊?rdquo;銆傝繖鍏朵腑灏卞埄鐢ㄥ埌浜嗘暎鍒楃爜锛?
鏁e垪琛ㄧ殑鏈川鏄€氳繃鏁扮粍瀹炵幇鐨勩€傚綋鎴戜滑瑕佽幏鍙栨暎鍒楄〃涓殑鏌愪釜“鍊?rdquo;鏃讹紝瀹為檯涓婃槸瑕佽幏鍙栨暟缁勪腑鐨勬煇涓綅缃殑鍏冪礌銆傝€屾暟缁勭殑浣嶇疆锛屽氨鏄€氳繃“閿?rdquo;鏉ヨ幏鍙栫殑锛涙洿杩涗竴姝ヨ锛屾暟缁勭殑浣嶇疆锛屾槸閫氳繃“閿?rdquo;瀵瑰簲鐨勬暎鍒楃爜璁$畻寰楀埌鐨勩€?

涓嬮潰锛屾垜浠互HashSet涓轰緥锛屾潵娣卞叆璇存槑hashCode()鐨勪綔鐢ㄣ€?/p>

鍋囪锛孒ashSet涓凡缁忔湁1000涓厓绱犮€傚綋鎻掑叆绗?001涓厓绱犳椂锛岄渶瑕佹€庝箞澶勭悊锛熷洜涓篐ashSet鏄疭et闆嗗悎锛屽畠鍏佽鏈夐噸澶嶅厓绱犮€?br /> “灏嗙1001涓厓绱犻€愪釜鐨勫拰鍓嶉潰1000涓厓绱犺繘琛屾瘮杈?rdquo;锛熸樉鐒讹紝杩欎釜鏁堢巼鏄浉绛変綆涓嬬殑銆傛暎鍒楄〃寰堝ソ鐨勮В鍐充簡杩欎釜闂锛屽畠鏍规嵁鍏冪礌鐨勬暎鍒楃爜璁$畻鍑哄厓绱犲湪鏁e垪琛ㄤ腑鐨勪綅缃紝鐒跺悗灏嗗厓绱犳彃鍏ヨ浣嶇疆鍗冲彲銆傚浜庣浉鍚岀殑鍏冪礌锛岃嚜鐒舵槸鍙繚瀛樹簡涓€涓€?br /> 鐢辨鍙煡锛岃嫢涓や釜鍏冪礌鐩哥瓑锛屽畠浠殑鏁e垪鐮佷竴瀹氱浉绛夛紱浣嗗弽杩囨潵纭笉涓€瀹氥€傚湪鏁e垪琛ㄤ腑锛?br /> 1銆佸鏋滀袱涓璞$浉绛夛紝閭d箞瀹冧滑鐨刪ashCode()鍊间竴瀹氳鐩稿悓锛?br /> 2銆佸鏋滀袱涓璞ashCode()鐩哥瓑锛屽畠浠苟涓嶄竴瀹氱浉绛夈€?br /> 娉ㄦ剰锛氳繖鏄湪鏁e垪琛ㄤ腑鐨勬儏鍐点€傚湪闈炴暎鍒楄〃涓竴瀹氬姝わ紒

瀵?ldquo;hashCode()鐨勪綔鐢?rdquo;灏辫皥杩欎箞澶氥€?/p>

绗?閮ㄥ垎 hashCode() 鍜?equals() 鐨勫叧绯?/h1>

鎺ヤ笅闈紝鎴戜滑璁ㄨ鍙﹀涓€涓瘽棰樸€傜綉涓婂緢澶氭枃绔犲皢 hashCode() 鍜?equals 鍏宠仈璧锋潵锛屾湁鐨勮鐨勪笉閫忓交锛屾湁璇璇昏€呯殑瀚岀枒銆傚湪杩欓噷锛屾垜鑷繁姊崇悊浜嗕竴涓?“hashCode() 鍜?equals()鐨勫叧绯?rdquo;銆?/p>

鎴戜滑浠?ldquo;绫荤殑鐢ㄩ€?rdquo;鏉ュ皢“hashCode() 鍜?equals()鐨勫叧绯?rdquo;鍒?绉嶆儏鍐垫潵璇存槑銆?/p>

  1. 绗竴绉?涓嶄細鍒涘缓“绫诲搴旂殑鏁e垪琛?rdquo;

      杩欓噷鎵€璇寸殑“涓嶄細鍒涘缓绫诲搴旂殑鏁e垪琛?rdquo;鏄锛氭垜浠笉浼氬湪HashSet, Hashtable, HashMap绛夌瓑杩欎簺鏈川鏄暎鍒楄〃鐨勬暟鎹粨鏋勪腑锛岀敤鍒拌绫汇€備緥濡傦紝涓嶄細鍒涘缓璇ョ被鐨凥ashSet闆嗗悎銆?
    
     鍦ㄨ繖绉嶆儏鍐典笅锛岃绫荤殑“hashCode() 鍜?equals() ”娌℃湁鍗婃瘺閽卞叧绯荤殑锛?
     杩欑鎯呭喌涓嬶紝equals() 鐢ㄦ潵姣旇緝璇ョ被鐨勪袱涓璞℃槸鍚︾浉绛夈€傝€宧ashCode() 鍒欐牴鏈病鏈変换浣曚綔鐢紝鎵€浠ワ紝涓嶇敤鐞嗕細hashCode()銆?
    

涓嬮潰锛屾垜浠€氳繃绀轰緥鏌ョ湅绫荤殑涓や釜瀵硅薄鐩哥瓑 浠ュ強 涓嶇瓑鏃秇ashCode()鐨勫彇鍊笺€?/p>

婧愮爜濡備笅 (NormalHashCodeTest.java)锛?/p>

import java.util.*;
import java.lang.Comparable;

/**
 * @desc 姣旇緝equals() 杩斿洖true 浠ュ強 杩斿洖false鏃讹紝 hashCode()鐨勫€笺€?
 *
 * @author skywang
 * @emai [email protected]
 */
public class NormalHashCodeTest{

    public static void main(String[] args) {
        // 鏂板缓2涓浉鍚屽唴瀹圭殑Person瀵硅薄锛?
        // 鍐嶇敤equals姣旇緝瀹冧滑鏄惁鐩哥瓑
        Person p1 = new Person("eee", 100);
        Person p2 = new Person("eee", 100);
        Person p3 = new Person("aaa", 200);
        System.out.printf("p1.equals(p2) : %s; p1(%d) p2(%d)
", p1.equals(p2), p1.hashCode(), p2.hashCode());
        System.out.printf("p1.equals(p3) : %s; p1(%d) p3(%d)
", p1.equals(p3), p1.hashCode(), p3.hashCode());
    }

    /**
     * @desc Person绫汇€?
     */
    private static class Person {
        int age;
        String name;

        public Person(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public String toString() {
            return name + " - " +age;
        }

        /** 
         * @desc 瑕嗙洊equals鏂规硶 
         */  
        public boolean equals(Object obj){  
            if(obj == null){  
                return false;  
            }  
              
            //濡傛灉鏄悓涓€涓璞¤繑鍥瀟rue锛屽弽涔嬭繑鍥瀎alse  
            if(this == obj){  
                return true;  
            }  
              
            //鍒ゆ柇鏄惁绫诲瀷鐩稿悓  
            if(this.getClass() != obj.getClass()){  
                return false;  
            }  
              
            Person person = (Person)obj;  
            return name.equals(person.name) && age==person.age;  
        } 
    }
}
  杩愯缁撴灉锛?
  p1.equals(p2) : true; p1(1169863946) p2(1901116749)
  p1.equals(p3) : false; p1(1169863946) p3(2131949076)

浠庣粨鏋滀篃鍙互鐪嬪嚭锛歱1鍜宲2鐩哥瓑鐨勬儏鍐典笅锛宧ashCode()涔熶笉涓€瀹氱浉绛夈€?/p>

  1. 绗簩绉?浼氬垱寤?ldquo;绫诲搴旂殑鏁e垪琛?rdquo;

     杩欓噷鎵€璇寸殑“浼氬垱寤虹被瀵瑰簲鐨勬暎鍒楄〃”鏄锛氭垜浠細鍦℉ashSet, Hashtable, HashMap绛夌瓑杩欎簺鏈川鏄暎鍒楄〃鐨勬暟鎹粨鏋勪腑锛岀敤鍒拌绫汇€備緥濡傦紝浼氬垱寤鸿绫荤殑HashSet闆嗗悎銆?
    
     鍦ㄨ繖绉嶆儏鍐典笅锛岃绫荤殑“hashCode() 鍜?equals() ”鏄湁鍏崇郴鐨勶細
     1)銆佸鏋滀袱涓璞$浉绛夛紝閭d箞瀹冧滑鐨刪ashCode()鍊间竴瀹氱浉鍚屻€?
           杩欓噷鐨勭浉绛夋槸鎸囷紝閫氳繃equals()姣旇緝涓や釜瀵硅薄鏃惰繑鍥?span class="hljs-literal">true銆?
     2)銆佸鏋滀袱涓璞ashCode()鐩哥瓑锛屽畠浠苟涓嶄竴瀹氱浉绛夈€?
            鍥犱负鍦ㄦ暎鍒楄〃涓紝hashCode()鐩哥瓑锛屽嵆涓や釜閿€煎鐨勫搱甯屽€肩浉绛夈€傜劧鑰屽搱甯屽€肩浉绛夛紝骞朵笉涓€瀹氳兘寰楀嚭閿€煎鐩哥瓑銆傝ˉ鍏呰涓€鍙ワ細“涓や釜涓嶅悓鐨勯敭鍊煎锛屽搱甯屽€肩浉绛?rdquo;锛岃繖灏辨槸鍝堝笇鍐茬獊銆?
    
     姝ゅ锛屽湪杩欑鎯呭喌涓嬨€傝嫢瑕佸垽鏂袱涓璞℃槸鍚︾浉绛夛紝闄や簡瑕佽鐩杄quals()涔嬪锛屼篃瑕佽鐩杊ashCode()鍑芥暟銆傚惁鍒欙紝equals()鏃犳晥銆?
    

渚嬪锛屽垱寤篜erson绫荤殑HashSet闆嗗悎锛屽繀椤诲悓鏃惰鐩朠erson绫荤殑equals() 鍜?hashCode()鏂规硶銆?br /> 濡傛灉鍗曞崟鍙槸瑕嗙洊equals()鏂规硶銆傛垜浠細鍙戠幇锛宔quals()鏂规硶娌℃湁杈惧埌鎴戜滑鎯宠鐨勬晥鏋溿€?/p>

鍙傝€冧唬鐮?(ConflictHashCodeTest1.java)锛?/p>

import java.util.*;
import java.lang.Comparable;

/**
 * @desc 姣旇緝equals() 杩斿洖true 浠ュ強 杩斿洖false鏃讹紝 hashCode()鐨勫€笺€?
 *
 * @author skywang
 * @emai [email protected]
 */
public class ConflictHashCodeTest1{

    public static void main(String[] args) {
        // 鏂板缓Person瀵硅薄锛?
        Person p1 = new Person("eee", 100);
        Person p2 = new Person("eee", 100);
        Person p3 = new Person("aaa", 200);

        // 鏂板缓HashSet瀵硅薄 
        HashSet set = new HashSet();
        set.add(p1);
        set.add(p2);
        set.add(p3);

        // 姣旇緝p1 鍜?p2锛?骞舵墦鍗板畠浠殑hashCode()
        System.out.printf("p1.equals(p2) : %s; p1(%d) p2(%d)
", p1.equals(p2), p1.hashCode(), p2.hashCode());
        // 鎵撳嵃set
        System.out.printf("set:%s
", set);
    }

    /**
     * @desc Person绫汇€?
     */
    private static class Person {
        int age;
        String name;

        public Person(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public String toString() {
            return "("+name + ", " +age+")";
        }

        /** 
         * @desc 瑕嗙洊equals鏂规硶 
         */  
        @Override
        public boolean equals(Object obj){  
            if(obj == null){  
                return false;  
            }  
              
            //濡傛灉鏄悓涓€涓璞¤繑鍥瀟rue锛屽弽涔嬭繑鍥瀎alse  
            if(this == obj){  
                return true;  
            }  
              
            //鍒ゆ柇鏄惁绫诲瀷鐩稿悓  
            if(this.getClass() != obj.getClass()){  
                return false;  
            }  
              
            Person person = (Person)obj;  
            return name.equals(person.name) && age==person.age;  
        } 
    }
}
  杩愯缁撴灉锛?
  p1.equals(p2) : true; p1(1169863946) p2(1690552137)
  set:[(eee, 100), (eee, 100), (aaa, 200)]

缁撴灉鍒嗘瀽锛?/p>

    鎴戜滑閲嶅啓浜哖erson鐨別quals()銆備絾鏄紝寰堝鎬殑鍙戠幇锛欻ashSet涓粛鐒舵湁閲嶅鍏冪礌锛歱1 鍜?p2銆備负浠€涔堜細鍑虹幇杩欑鎯呭喌鍛紵

    杩欐槸鍥犱负铏界劧p1 鍜?p2鐨勫唴瀹圭浉绛夛紝浣嗘槸瀹冧滑鐨刪ashCode()涓嶇瓑锛涙墍浠ワ紝HashSet鍦ㄦ坊鍔爌1鍜宲2鐨勬椂鍊欙紝璁や负瀹冧滑涓嶇浉绛夈€?

涓嬮潰锛屾垜浠悓鏃惰鐩杄quals() 鍜?hashCode()鏂规硶銆?/p>

鍙傝€冧唬鐮?(ConflictHashCodeTest2.java)锛?/p>

import java.util.*;
import java.lang.Comparable;

/**
 * @desc 姣旇緝equals() 杩斿洖true 浠ュ強 杩斿洖false鏃讹紝 hashCode()鐨勫€笺€?
 *
 * @author skywang
 * @emai [email protected]
 */
public class ConflictHashCodeTest2{

    public static void main(String[] args) {
        // 鏂板缓Person瀵硅薄锛?
        Person p1 = new Person("eee", 100);
        Person p2 = new Person("eee", 100);
        Person p3 = new Person("aaa", 200);
        Person p4 = new Person("EEE", 100);

        // 鏂板缓HashSet瀵硅薄 
        HashSet set = new HashSet();
        set.add(p1);
        set.add(p2);
        set.add(p3);

        // 姣旇緝p1 鍜?p2锛?骞舵墦鍗板畠浠殑hashCode()
        System.out.printf("p1.equals(p2) : %s; p1(%d) p2(%d)
", p1.equals(p2), p1.hashCode(), p2.hashCode());
        // 姣旇緝p1 鍜?p4锛?骞舵墦鍗板畠浠殑hashCode()
        System.out.printf("p1.equals(p4) : %s; p1(%d) p4(%d)
", p1.equals(p4), p1.hashCode(), p4.hashCode());
        // 鎵撳嵃set
        System.out.printf("set:%s
", set);
    }

    /**
     * @desc Person绫汇€?
     */
    private static class Person {
        int age;
        String name;

        public Person(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public String toString() {
            return name + " - " +age;
        }

        /** 
         * @desc閲嶅啓hashCode 
         */  
        @Override
        public int hashCode(){  
            int nameHash =  name.toUpperCase().hashCode();
            return nameHash ^ age;
        }

        /** 
         * @desc 瑕嗙洊equals鏂规硶 
         */  
        @Override
        public boolean equals(Object obj){  
            if(obj == null){  
                return false;  
            }  
              
            //濡傛灉鏄悓涓€涓璞¤繑鍥瀟rue锛屽弽涔嬭繑鍥瀎alse  
            if(this == obj){  
                return true;  
            }  
              
            //鍒ゆ柇鏄惁绫诲瀷鐩稿悓  
            if(this.getClass() != obj.getClass()){  
                return false;  
            }  
              
            Person person = (Person)obj;  
            return name.equals(person.name) && age==person.age;  
        } 
    }
}
杩愯缁撴灉锛?
p1.equals(p2) : true; p1(68545) p2(68545)
p1.equals(p4) : false; p1(68545) p4(68545)
set:[aaa - 200, eee - 100]
缁撴灉鍒嗘瀽锛?
    杩欎笅锛宔quals()鐢熸晥浜嗭紝HashSet涓病鏈夐噸澶嶅厓绱犮€?
    姣旇緝p1鍜宲2锛屾垜浠彂鐜帮細瀹冧滑鐨刪ashCode()鐩哥瓑锛岄€氳繃equals()姣旇緝瀹冧滑涔熻繑鍥?span class="hljs-literal">true銆傛墍浠ワ紝p1鍜宲2琚涓虹浉绛夈€?
    姣旇緝p1鍜宲4锛屾垜浠彂鐜帮細铏界劧瀹冧滑鐨刪ashCode()鐩哥瓑锛涗絾鏄紝閫氳繃equals()姣旇緝瀹冧滑杩斿洖false銆傛墍浠ワ紝p1鍜宲4琚涓轰笉鐩哥瓑銆?/span>

以上是关于hashCode() 鍜宔quals() 鍖哄埆鍜屼綔鐢紙杞級的主要内容,如果未能解决你的问题,请参考以下文章

js涓璫onst,var,let鍖哄埆

鎷︽埅鍣ㄥ拰杩囨护鍣ㄧ殑鍖哄埆

I甯у拰IDR甯х殑鍖哄埆

澶ф暟鎹椂浠o紝鑻规灉鎵嬫満鍜屽畨鍗撴墜鏈虹殑鍖哄埆!

docker瀹瑰櫒涓殑EntryPoint鍜孋MD鍛戒护鍖哄埆

DOM0绾т笌DOM2绾х殑鍖哄埆