Effective Java



Download 2,19 Mb.
Pdf ko'rish
bet221/341
Sana11.07.2022
Hajmi2,19 Mb.
#776765
1   ...   217   218   219   220   221   222   223   224   ...   341
Bog'liq
Effective Java



CHAPTER 8
METHODS
248
same 
immutable
empty collection repeatedly, as immutable objects may be shared
freely (Item 17). Here is the code to do it, using the 
Collections.emptyList
method. If you were returning a set, you’d use 
Collections.emptySet
; if you
were returning a map, you’d use 
Collections.emptyMap
. But remember, this is
an optimization, and it’s seldom called for. If you think you need it, measure
performance before and after, to ensure that it’s actually helping:
// Optimization - avoids allocating empty collections
public List getCheeses() {
return cheesesInStock.isEmpty() ? Collections.emptyList()
: new ArrayList<>(cheesesInStock);
}
The situation for arrays is identical to that for collections. Never return null
instead of a zero-length array. Normally, you should simply return an array of the
correct length, which may be zero. Note that we’re passing a zero-length array
into the 
toArray
method to indicate the desired return type, which is 
Cheese[]
:
//The right way to return a possibly empty array
public Cheese[] getCheeses() {
return cheesesInStock.toArray(new Cheese[0]);
}
If you believe that allocating zero-length arrays is harming performance, you
can return the same zero-length array repeatedly because all zero-length arrays are
immutable:
// Optimization - avoids allocating empty arrays
private static final Cheese[] EMPTY_CHEESE_ARRAY = new Cheese[0];
public Cheese[] getCheeses() {
return cheesesInStock.toArray(EMPTY_CHEESE_ARRAY);
}
In the optimized version, we pass 
the same
empty array into every 
toArray
call,
and this array will be returned from 
getCheeses
whenever 
cheesesInStock
is
empty. Do 
not
preallocate the array passed to 
toArray
in hopes of improving
performance. Studies have shown that it is counterproductive [Shipilëv16]:

Download 2,19 Mb.

Do'stlaringiz bilan baham:
1   ...   217   218   219   220   221   222   223   224   ...   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