— Scott McNealy, Sun Microsystems Chairman, President and ceo


HashSet songSet = new HashSet()



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

HashSet songSet = new HashSet();
songSet.addAll(songList); 
System.out.println(songSet);
}
// getSongs() and addSong() methods
}
File Edit Window Help GetBetterMusic
%java Jukebox6
[Pink Moon, Somersault, Shiva Moon, Circles, Deep Channel, 
Passenger, Listen, Listen, Listen, Circles]
[Circles, Circles, Deep Channel, Listen, Listen, Listen, 
Passenger, Pink Moon, Shiva Moon, Somersault]
[Pink Moon, Listen, Shiva Moon, Circles, Listen, Deep Channel, 
Passenger, Circles, Listen, Somersault]
Here we create a new HashSet 
parameterized to hold Songs.
We didn’t change getSongs(), so it still puts the songs in an ArrayList
HashSet has a simple addAll() method that can 
take another collection and use it to populate 
the HashSet. It’s the same as if we added each 
song one at a time (except much simpler).
The Set didn’t help!!
We still have all the duplicates!
Before sorting 
the ArrayList.
After sorting 
the ArrayList 
(by title).
After putting it 
into a HashSet, 
and printing the 
HashSet (we didn’t 
call sort() again).
(And it lost its sort order 
when we put the list into a 
HashSet, but we’ll worry about 
that one later...)


560
 
chapter 16
What makes t wo objects equal?
First, we have to ask—what makes two Song references 
duplicates? They must be considered equal. Is it simply two 
references to the very same object, or is it two separate objects 
that both have the same title?
This brings up a key issue: reference equality vs. object equality.
$
Reference
equality
 

Download 40,57 Mb.

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