— Scott McNealy, Sun Microsystems Chairman, President and ceo



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

}
}
class Test {
public void go() {
Book b1 = new Book(“How Cats Work”);
Book b2 = new Book(“Remix your Body”);
Book b3 = new Book(“Finding Emo”);
BookCompare bCompare = new BookCompare();
TreeSet tree = new TreeSet(bCompare);
tree.add(new Book(“How Cats Work”);
tree.add(new Book(“Finding Emo”);
tree.add(new Book(“Remix your Body”);
System.out.println(tree);
}
}
how TreeSets 
sort


collections 
with 
generics
you are here
4
567
We’ve seen Lists and Sets, now we’ll use a Map
Lists and Sets are great, but sometimes a Map is the best collection (not Collection 
with a capital “C”—remember that Maps are part of Java collections but they don’t 
implement the Collection interface).
Imagine you want a collection that acts like a property list, where you give it a name 
and it gives you back the value associated with that name. Although keys will often be 
Strings, they can be any Java object (or, through autoboxing, a primitive).
import java.util.*;
public class TestMap {
public static void main(String[] args) {
HashMap

 scores = new HashMap

();
scores.
put(“Kathy”, 42);
scores.
put(“Bert”, 343);
scores.
put(“Skyler”, 420);
System.out.println(scores);
System.out.println(
scores.
get(“Bert”)
);
}
}
Map
“Ball” “Fish” “Car”
“Ball1” “Ball2” “Ball3” “Ball4”
Each element in a Map is actually 
TWO objects—a 
key and a value.
You can have duplicate 
values, but 
NOT duplicate 
keys.
Map example
HashMap needs TWO type parameters—
one for the key and one for the value.
Use put() instead of add(), and now of course 
it takes two arguments (key, value). 

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   522   523   524   525   526   527   528   529   ...   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