— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet520/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   516   517   518   519   520   521   522   523   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

hashCode()
742
hashCode()
742
title: Circles
hashCode: 742
title: Circles
hashCode: 742
HashSet
Your hashcodes 
are the same, but are 
you REALLY equal?
Object you’re trying 
to add runs its equals() 
method, comparing itself 
to bar, and returns true.
Object already IN 
the HashSet.
Song
foo
Song
bar
equals( bar )
true


562
 
chapter 16
The Song class with overridden 
hashCode() and equals()
class Song implements Comparable{
String title;
String artist;
String rating;
String bpm;
public boolean 
equals(Object aSong)
 {
Song s = (Song) aSong;
return getTitle().equals(s.getTitle());
}
public int 
hashCode()
 {
return title.hashCode();
}
public int compareTo(Song s) {
return title.compareTo(s.getTitle());
}
Song(String t, String a, String r, String b) {
title = t;
artist = a;
rating = r;
bpm = b;
}
public String getTitle() {
return title;
}
public String getArtist() { 
return artist;
}
public String getRating() {
return rating;
}
public String getBpm() {
return bpm;
}
public String toString() {
return title; 
}
}
The HashSet (or anyone else calling this 
method) sends it another Song.
The GREAT news is that title is a String, 
and Strings have an overridden equals() 
method. So all we have to do is ask one 
title if it’s equal to the other song’s title.
Same deal here... the String class has an overridden 
hashCode() method, so you can just return the result of 
calling hashCode() on the title. Notice how hashCode() 
and equals() are using the SAME instance variable.

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   516   517   518   519   520   521   522   523   ...   555




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish