更新時(shí)間:2019-08-30 10:56:50 來(lái)源:動(dòng)力節(jié)點(diǎn) 瀏覽3441次
今天動(dòng)力節(jié)點(diǎn)java培訓(xùn)機(jī)構(gòu)小編為大家介紹“JAVA中l(wèi)ist.contains()方法,要重寫(xiě)equals(),hashcode()方法”,希望能夠幫助正在學(xué)習(xí)java的零基礎(chǔ)學(xué)員,下面就隨小編一起看看吧。
object對(duì)象中的 public boolean equals(Object obj),對(duì)于任何非空引用值 x 和 y,當(dāng)且僅當(dāng) x 和 y 引用同一個(gè)對(duì)象時(shí),此方法才返回 true;
注意:當(dāng)此方法被重寫(xiě)時(shí),通常有必要重寫(xiě) hashCode 方法,以維護(hù) hashCode 方法的常規(guī)協(xié)定,該協(xié)定聲明相等對(duì)象必須具有相等的哈希碼。如下:
(1)當(dāng)obj1.equals(obj2)為true時(shí),obj1.hashCode() == obj2.hashCode()必須為true
(2)當(dāng)obj1.hashCode() == obj2.hashCode()為false時(shí),obj1.equals(obj2)必須為false
如果不重寫(xiě)equals,那么比較的將是對(duì)象的引用是否指向同一塊內(nèi)存地址,重寫(xiě)之后目的是為了比較兩個(gè)對(duì)象的value值是否相等。特別指出利用equals比較八大包裝對(duì)象
(如int,float等)和String類(lèi)(因?yàn)樵擃?lèi)已重寫(xiě)了equals和hashcode方法)對(duì)象時(shí),默認(rèn)比較的是值,在比較其它自定義對(duì)象時(shí)都是比較的引用地址
hashcode是用于散列數(shù)據(jù)的快速存取,如利用HashSet/HashMap/Hashtable類(lèi)來(lái)存儲(chǔ)數(shù)據(jù)時(shí),都是根據(jù)存儲(chǔ)對(duì)象的hashcode值來(lái)進(jìn)行判斷是否相同的。
這樣如果我們對(duì)一個(gè)對(duì)象重寫(xiě)了euqals,意思是只要對(duì)象的成員變量值都相等那么euqals就等于true,但不重寫(xiě)hashcode,那么我們?cè)賜ew一個(gè)新的對(duì)象,
當(dāng)原對(duì)象.equals(新對(duì)象)等于true時(shí),兩者的hashcode卻是不一樣的,由此將產(chǎn)生了理解的不一致,如在存儲(chǔ)散列集合時(shí)(如Set類(lèi)),將會(huì)存儲(chǔ)了兩個(gè)值一樣的對(duì)象,
導(dǎo)致混淆,因此,就也需要重寫(xiě)hashcode()
舉例說(shuō)明:
import java.util.*;
public class HelloWorld {
public static void main(String[] args) {
/*
Collection c = new HashSet();
c.add("hello");
c.add(new Name("f1","l1"));
c.add(new Integer(100));
c.remove("hello");
c.remove(new Integer(100));
System.out.println(c.remove(new Name("f1","l1")));
*/
Name n1 = new Name("01");
Name n2 = new Name("01");
Collection c = new HashSet();
c.add(n1);
System.out.println("------------");
c.add(n2);
System.out.println("------------");
System.out.println(n1.equals(n2));
System.out.println("------------");
System.out.println(n1.hashCode());
System.out.println(n2.hashCode());
System.out.println(c);
}
}
class Name {
private String id;
public Name(String id) {
this.id = id;
}
public String toString(){
return this.id;
}
public boolean equals(Object obj) {
if (obj instanceof Name) {
Name name = (Name) obj;
System.out.println("equal"+ name.id);
return (id.equals(name.id));
}
return super.equals(obj);
}
public int hashCode() {
Name name = (Name) this;
System.out.println("Hash" + name.id);
return id.hashCode();
}
}
image.png
就這個(gè)程序進(jìn)行分析,在第一次添加時(shí),調(diào)用了hashcode()方法,將hashcode存入對(duì)象中,第二次也一樣,然后對(duì)hashcode進(jìn)行比較。hashcode也只用于HashSet/HashMap/Hashtable類(lèi)存儲(chǔ)數(shù)據(jù),所以會(huì)用于比較,需要重寫(xiě)
總結(jié),自定義類(lèi)要重寫(xiě)equals方法來(lái)進(jìn)行等值比較,自定義類(lèi)要重寫(xiě)compareTo方法來(lái)進(jìn)行不同對(duì)象大小的比較,重寫(xiě)hashcode方法為了將數(shù)據(jù)存入HashSet/HashMap/Hashtable類(lèi)時(shí)進(jìn)行比較。
以上就是動(dòng)力節(jié)點(diǎn)java培訓(xùn)機(jī)構(gòu)介紹的“JAVA中l(wèi)ist.contains()方法,要重寫(xiě)equals(),hashcode()方法”的內(nèi)容,希望能夠幫助到大家,更多精彩內(nèi)容請(qǐng)繼續(xù)關(guān)注動(dòng)力節(jié)點(diǎn)java培訓(xùn)機(jī)構(gòu)官網(wǎng),每天會(huì)有精彩內(nèi)容分享與你。
相關(guān)免費(fèi)視頻教程推薦
List存儲(chǔ)自定義類(lèi)型數(shù)據(jù)需要重寫(xiě)equals方法(視頻教程下載):http://www.dabaquan.cn/xiazai/2487.html
相關(guān)閱讀
0基礎(chǔ) 0學(xué)費(fèi) 15天面授
有基礎(chǔ) 直達(dá)就業(yè)
業(yè)余時(shí)間 高薪轉(zhuǎn)行
工作1~3年,加薪神器
工作3~5年,晉升架構(gòu)
提交申請(qǐng)后,顧問(wèn)老師會(huì)電話(huà)與您溝通安排學(xué)習(xí)
初級(jí) 202925
初級(jí) 203221
初級(jí) 202629
初級(jí) 203743