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>
- “娌℃湁瑕嗙洊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銆?
- "瑕嗙洊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>
-
绗竴绉?涓嶄細鍒涘缓“绫诲搴旂殑鏁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>
-
绗簩绉?浼氬垱寤?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() 鍖哄埆鍜屼綔鐢紙杞級的主要内容,如果未能解决你的问题,请参考以下文章