Effective Java


// Simple program to exercise generic method



Download 2,19 Mb.
Pdf ko'rish
bet125/341
Sana11.07.2022
Hajmi2,19 Mb.
#776765
1   ...   121   122   123   124   125   126   127   128   ...   341
Bog'liq
Effective Java

// Simple program to exercise generic method
public static void main(String[] args) {
Set guys = Set.of("Tom", "Dick", "Harry");
Set stooges = Set.of("Larry", "Moe", "Curly");
Set aflCio = union(guys, stooges);
System.out.println(aflCio);
}
When you run the program, it prints 
[Moe,
Tom,
Harry,
Larry,
Curly,
Dick]
.
(The order of the elements in the output is implementation-dependent.)
A limitation of the 
union
method is that the types of all three sets (both input
parameters and the return value) have to be exactly the same. You can make the
method more flexible by using 
bounded wildcard types
(Item 31).
On occasion, you will need to create an object that is immutable but
applicable to many different types. Because generics are implemented by erasure
(Item 28), you can use a single object for all required type parameterizations, but
you need to write a static factory method to repeatedly dole out the object for each
requested type parameterization. This pattern, called the 
generic singleton factory
,
is used for function objects (Item 42) such as 
Collections.reverseOrder
, and
occasionally for collections such as 
Collections.emptySet
.
Suppose that you want to write an identity function dispenser. The libraries
provide 
Function.identity
, so there’s no reason to write your own (Item 59),
but it is instructive. It would be wasteful to create a new identity function object
time one is requested, because it’s stateless. If Java’s generics were reified, you
would need one identity function per type, but since they’re erased a generic
singleton will suffice. Here’s how it looks:
// Generic singleton factory pattern
private static UnaryOperator IDENTITY_FN = (t) -> t;

Download 2,19 Mb.

Do'stlaringiz bilan baham:
1   ...   121   122   123   124   125   126   127   128   ...   341




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