Effective Java



Download 2,19 Mb.
Pdf ko'rish
bet133/341
Sana11.07.2022
Hajmi2,19 Mb.
#776765
1   ...   129   130   131   132   133   134   135   136   ...   341
Bog'liq
Effective Java

Comparable
> T max(
List
list)
To get the revised declaration from the original, we applied the PECS heuris-
tic twice. The straightforward application is to the parameter 
list
. It produces 
T
instances, so we change the type from 
List
to 
Listextends
T>
. The tricky
application is to the type parameter 
T
. This is the first time we’ve seen a wildcard
applied to a type parameter. Originally, 
T
was specified to extend 
Comparable
,
but a comparable of 
T
consumes 
T
instances (and produces integers indicating
order relations). Therefore, the parameterized type 
Comparable
is replaced by
the bounded wildcard type 
Comparablesuper
T>
. Comparables are always
consumers, so
 
you should generally 
use 
Comparable
super
T>
 in preference to
Comparable
.
The same is true of comparators; therefore, you should generally
use 
Comparator
super
T>
 in preference to 
Comparator
.
The revised 
max
declaration is probably the most complex method declaration
in this book. Does the added complexity really buy you anything? Again, it does.
Here is a simple example of a list that would be excluded by the original declara-
tion but is permitted by the revised one:
List> scheduledFutures = ... ;
The reason that you can’t apply the original method declaration to this list is
that 
ScheduledFuture
does not implement 
Comparable
.
Instead, it is a subinterface of 
Delayed
, which extends 
Comparable
. In
other words, a 
ScheduledFuture
instance isn’t merely comparable to other


CHAPTER 5
GENERICS
144
ScheduledFuture
instances; it is comparable to any 
Delayed
instance, and that’s
enough to cause the original declaration to reject it. More generally, the wildcard
is required to support types that do not implement 
Comparable
(or 
Comparator
)
directly but extend a type that does.
There is one more wildcard-related topic that bears discussing. There is a
duality between type parameters and wildcards, and many methods can be
declared using one or the other. For example, here are two possible declarations
for a static method to swap two indexed items in a list. The first uses an
unbounded type parameter (Item 30) and the second an unbounded wildcard:

Download 2,19 Mb.

Do'stlaringiz bilan baham:
1   ...   129   130   131   132   133   134   135   136   ...   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