Effective Java


// Using a recursive type bound to express mutual comparability



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

// Using a recursive type bound to express mutual comparability
public static 
>
E max(Collection c);


CHAPTER 5
GENERICS
138
The type bound 
extends
Comparable>
may be read as “any type 
E
that can
be compared to itself,” which corresponds more or less precisely to the notion of
mutual comparability. 
Here is a method to go with the previous declaration. It calculates the maxi-
mum value in a collection according to its elements’ natural order, and it compiles
without errors or warnings:
// Returns max value in a collection - uses recursive type bound
public static > E max(Collection c) {
if (c.isEmpty())
throw new IllegalArgumentException("Empty collection");
E result = null;
for (E e : c)
if (result == null || e.compareTo(result) > 0)
result = Objects.requireNonNull(e);
return result;
}
Note that this method throws 
IllegalArgumentException
if the list is empty. A
better alternative would be to return an 
Optional
(Item 55).
Recursive type bounds can get much more complex, but luckily they rarely
do. If you understand this idiom, its wildcard variant (Item 31), and the 
simulated
self-type
idiom (Item 2), you’ll be able to deal with most of the recursive type
bounds you encounter in practice.
In summary, generic methods, like generic types, are safer and easier to use
than methods requiring their clients to put explicit casts on input parameters and
return values. Like types, you should make sure that your methods can be used
without casts, which often means making them generic. And like types, you
should generify existing methods whose use requires casts. This makes life easier
for new users without breaking existing clients (Item 26).


ITEM 31: USE BOUNDED WILDCARDS TO INCREASE API FLEXIBILITY
139

Download 2,19 Mb.

Do'stlaringiz bilan baham:
1   ...   123   124   125   126   127   128   129   130   ...   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