Effective Java


// List as a typesafe alternative to a generic varargs parameter



Download 2,19 Mb.
Pdf ko'rish
bet139/341
Sana11.07.2022
Hajmi2,19 Mb.
#776765
1   ...   135   136   137   138   139   140   141   142   ...   341
Bog'liq
Effective Java

// List as a typesafe alternative to a generic varargs parameter
static  List flatten(
List> lists
) {
List result = new ArrayList<>();
for (List list : lists)
result.addAll(list);
return result;
}


CHAPTER 5
GENERICS
150
This method can then be used in conjunction with the static factory method
List.of
to allow for a variable number of arguments. Note that this approach
relies on the fact that the 
List.of
declaration is annotated with 
@SafeVarargs
:
audience = flatten(List.of(friends, romans, countrymen));
The advantage of this approach is that the compiler can 
prove
that the method
is typesafe. You don’t have to vouch for its safety with a 
SafeVarargs
annotation,
and you don’t have worry that you might have erred in determining that it was
safe. The main disadvantage is that the client code is a bit more verbose and may
be a bit slower.
This trick can also be used in situations where it is impossible to write a safe
varargs method, as is the case with the 
toArray
method on page 147. Its 
List
ana-
logue 
is
the 
List.of
method, so we don’t even have to write it; the Java libraries
authors have done the work for us. The 
pickTwo
method then becomes this:
static  
List
pickTwo(T a, T b, T c) {
switch(rnd.nextInt(3)) {
case 0: return 
List.of(a, b)
;
case 1: return 
List.of(a, c)
;
case 2: return 
List.of(b, c)
;
}
throw new AssertionError();
}
and the main method becomes this:
public static void main(String[] args) {
List
attributes = pickTwo("Good", "Fast", "Cheap");
}
The resulting code is typesafe because it uses only generics, and not arrays.
In summary, varargs and generics do not interact well because the varargs
facility is a leaky abstraction built atop arrays, and arrays have different type rules
from generics. Though generic varargs parameters are not typesafe, they are legal.
If you choose to write a method with a generic (or parameterized) varargs parame-
ter, first ensure that the method is typesafe, and then annotate it with 
@Safe-
Varargs
so it is not unpleasant to use.


ITEM 33: CONSIDER TYPESAFE HETEROGENEOUS CONTAINERS
151

Download 2,19 Mb.

Do'stlaringiz bilan baham:
1   ...   135   136   137   138   139   140   141   142   ...   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