— Scott McNealy, Sun Microsystems Chairman, President and ceo



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

564
 
chapter 16
And if we want the set to stay 
sorted, we’ve got TreeSet
import java.util.*;
import java.io.*;
public class Jukebox8 { 
ArrayList songList = new ArrayList();
int val;
public static void main(String[] args) {
new Jukebox8().go();
}
public void go() { 
getSongs(); 
System.out.println(songList);
Collections.sort(songList);
System.out.println(songList);
TreeSet songSet = new TreeSet();
songSet.addAll(songList);
System.out.println(songSet); 
}
void getSongs() {
try {
File file = new File(“SongListMore.txt”);
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = null;
while ((line= reader.readLine()) != null) {
addSong(line);
}
} catch(Exception ex) {
ex.printStackTrace();
}
}
void addSong(String lineToParse) {
String[] tokens = lineToParse.split(“/”);
Song nextSong = new Song(tokens[0], tokens[1], tokens[2], tokens[3]);
songList.add(nextSong); 
}
}
TreeSet is similar to HashSet in that it prevents duplicates. But it also keeps the list sorted. It works 
just like the sort() method in that if you make a TreeSet using the set’s no-arg constructor, the 
TreeSet uses each object’s compareTo() method for the sort. But you have the option of passing 
a Comparator to the TreeSet constructor, to have the TreeSet use that instead. The downside to 
TreeSet is that if you don’t need sorting, you’re still paying for it with a small performance hit. But 
you’ll probably find that the hit is almost impossible to notice for most apps.
Instantiate a TreeSet instead of HashSet. 
Calling the no-arg TreeSet constructor 
means the set will use the Song object’s 
compareTo() method for the sort.
(We could have passed in a Comparator.)
We can add all the songs from the HashSet 
using addAll(). (Or we could have added the 
songs individually using songSet.add() just 
the way we added songs to the ArrayList.)

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   519   520   521   522   523   524   525   526   ...   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