— Scott McNealy, Sun Microsystems Chairman, President and ceo



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

object 
equality


collections 
with 
generics
you are here
4
561
title: Circles
hashCode: 742
title: Circles
hashCode: 742
How a HashSet checks for duplicates: hashCode() and equals()
When you put an object into a Hashset, it uses the 
object’s hashcode value to determine where to put 
the object in the Set. But it also compares the object’s 
hashcode to the hashcode of all the other objects in 
the HashSet, and if there’s no matching hashcode, 
the HashSet assumes that this new object is not a 
duplicate. 
In other words, if the hashcodes are different, the 
HashSet assumes there’s no way the objects can be 
equal!
So you must override hashCode() to make sure the 
objects have the same value.
But two objects with the same hashCode() might not 
be equal (more on this on the next page), so if the 
HashSet finds a matching hashcode for two objects—
one you’re inserting and one already in the set—the 
HashSet will then call one of the object’s equals() 
methods to see if these hashcode-matched objects 
really are equal.
And if they’re equal, the HashSet knows that the 
object you’re attempting to add is a duplicate of 
something in the Set, so the add doesn’t happen. 
You don’t get an exception, but the HashSet’s add() 
method returns a boolean to tell you (if you care) 
whether the new object was added. So if the add() 
method returns false, you know the new object was a 
duplicate of something already in the set.
HashSet
I need to know 
if your hashcode 
values are the same.
Object you’re trying to 
add to the HashSet.
Object already IN 
the HashSet.
Song
foo
Song
bar

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   515   516   517   518   519   520   521   522   ...   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