Effective Java



Download 2,19 Mb.
Pdf ko'rish
bet157/341
Sana11.07.2022
Hajmi2,19 Mb.
#776765
1   ...   153   154   155   156   157   158   159   160   ...   341
Bog'liq
Effective Java



ITEM 37: USE ENUMMAP INSTEAD OF ORDINAL INDEXING
173
parameter form of 
Collectors.groupingBy
, which allows the caller to specify
the map implementation using the 
mapFactory
parameter:
// Using a stream and an EnumMap to associate data with an enum
System.out.println(Arrays.stream(garden)
.collect(groupingBy(p -> p.lifeCycle,
() -> new EnumMap<>(LifeCycle.class)
, toSet())));
This optimization would not be worth doing in a toy program like this one but
could be critical in a program that made heavy use of the map.
The behavior of the stream-based versions differs slightly from that of the
EmumMap
version. The 
EnumMap
version always makes a nested map for each plant
lifecycle, while the stream-based versions only make a nested map if the garden
contains one or more plants with that lifecycle. So, for example, if the garden con-
tains annuals and perennials but no biennials, the size of 
plantsByLifeCycle
will
be three in the 
EnumMap
version and two in both of the stream-based versions.
You may see an array of arrays indexed (twice!) by ordinals used to represent
a mapping from two enum values. For example, this program uses such an array to
map two phases to a phase transition (liquid to solid is freezing, liquid to gas is
boiling, and so forth):

Download 2,19 Mb.

Do'stlaringiz bilan baham:
1   ...   153   154   155   156   157   158   159   160   ...   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