— Scott McNealy, Sun Microsystems Chairman, President and ceo


interface collections



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

interface


collections 
with 
generics
you are here
4
553
import java.util.*;
import java.io.*;
public class Jukebox5 { 
ArrayList songList = new ArrayList(); 
public static void main(String[] args) {
new Jukebox5().go();
}
class ArtistCompare implements 
Comparator
 {
public int 
compare(Song one, Song two
) { 
return one.getArtist().compareTo(two.getArtist());
}
}
public void go() { 
getSongs(); 
System.out.println(songList);
Collections.sort(songList);
System.out.println(songList);
ArtistCompare artistCompare = new ArtistCompare();
Collections.sort(songList, artistCompare);
System.out.println(songList); 
}
void getSongs() {
// I/O code here
}
void addSong(String lineToParse) {
// parse line and add to song list 
}
}
Create a new inner class that implements 
Comparator (note that its type 
parameter matches the type we’re going 
to compare—in this case Song objects.)
Updating the Jukebox to use a Comparator
We did three new things in this code:
1) Created an inner class that implements Comparator (and thus the compare() 
method that does the work previously done by compareTo()).
2) Made an instance of the Comparator inner class.
3) Called the overloaded sort() method, giving it both the song list and the 
instance of the Comparator inner class.
Note: we also updated the Song class toString() method to print both the song 
title and the artist. (It prints title: artist regardless of how the list is sorted.)
We’re letting the String variables (for artist) 
do the actual comparison, since Strings already 
know how to alphabetize themselves.
Make an instance of the 
Comparator inner class.
Invoke sort(), passing it the list 
and a reference to the new 
custom Comparator object.
Note: we’ve made sort-by-title the default sort, by 
keeping the compareTo() method in Song use the 
titles. But another way to design this would be to 
implement both the title sorting and artist sorting as 
inner Comparator classes, and not have Song implement 
Comparable at all. That means we’d always use the two-
arg version of Collections.sort().
This becomes a String (the artist)



Download 40,57 Mb.

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