— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet509/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   505   506   507   508   509   510   511   512   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

%java Jukebox3
[Pink Moon, Somersault, Shiva Moon, Circles, Deep 
Channel, Passenger, Listen]
[Circles, Deep Channel, Listen, Passenger, Pink 
Moon, Shiva Moon, Somersault]
This time it worked. It prints the list, then calls sort 
which puts the Songs in alphabetical order by title.
the Comparable 
interface


collections 
with 
generics
you are here
4
551
We can sort the list, but...
Look at the Collections class API again. There’s a 
second sort() method—and it takes a Comparator.
There’s a new problem—Lou wants two different views of the song list, 
one by song title and one by artist!
But when you make a collection element comparable (by having it 
implement Comparable), you get only one chance to implement the 
compareTo() method. So what can you do? 
The horrible way would be to use a fl ag variable in the Song class, 
and then do an if test in compareTo() and give a different result 
depending on whether the fl ag is set to use title or artist for the 
comparison.
But that’s an awful and brittle solution, and there’s something much 
better. Something built into the API for just this purpose—when you 
want to sort the same thing in more than one way.
The sort() method
is overloaded to 
take something cal
led a Comparator.
Note to self: figur
e out how to 
get /make a Compa
rator that can 
compare and order
the songs by 
artist instead of t
itle...
That’s not good enough. 
Sometimes I want it to sort 
by artist instead of title. 


552
 
chapter 16
Using a custom Comparator
An element in a list can compare itself to another of 
its own type in only one way, using its compareTo() 
method. But a Comparator is external to the element 
type you’re comparing—it’s a separate class. So you can 
make as many of these as you like! Want to compare 
songs by artist? Make an ArtistComparator. Sort by beats 
per minute? Make a BPMComparator. 
Then all you need to do is call the overloaded sort() 
method that takes the List and the Comparator that will 
help the sort() method put things in order.
The sort() method that takes a Comparator will use the 
Comparator instead of the element’s own compareTo() 
method, when it puts the elements in order. In other 
words, if your sort() method gets a Comparator, it won’t 
even call the compareTo() method of the elements 
in the list. The sort() method will instead invoke the 

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   505   506   507   508   509   510   511   512   ...   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