Effective Java



Download 2,19 Mb.
Pdf ko'rish
bet192/341
Sana11.07.2022
Hajmi2,19 Mb.
#776765
1   ...   188   189   190   191   192   193   194   195   ...   341
Bog'liq
Effective Java



ITEM 46: PREFER SIDE-EFFECT-FREE FUNCTIONS IN STREAMS
211
This snippet does the same thing as the previous one but makes proper use of
the streams API. It’s shorter and clearer. So why would anyone write it the other
way? Because it uses tools they’re already familiar with. Java programmers know
how to use for-each loops, and the 
forEach
terminal operation is similar. But the
forEach
operation is among the least powerful of the terminal operations and the
least stream-friendly. It’s explicitly iterative, and hence not amenable to paral-
lelization. 
The 
forEach
 operation should be used only to report the result of a
stream computation, not to perform the computation. 
Occasionally, it makes
sense to use 
forEach
for some other purpose, such as adding the results of a
stream computation to a preexisting collection.
The improved code uses a 
collector
, which is a new concept that you have to
learn in order to use streams. The 
Collectors
API is intimidating: it has thirty-
nine methods, some of which have as many as five type parameters. The good
news is that you can derive most of the benefit from this API without delving into
its full complexity. For starters, you can ignore the 
Collector
interface and think
of a collector as an opaque object that encapsulates a 
reduction
strategy. In this
context, reduction means combining the elements of a stream into a single object.
The object produced by a collector is typically a collection (which accounts for
the name collector). 
The collectors for gathering the elements of a stream into a true 
Collection
are straightforward. There are three such collectors: 
toList()

toSet()
, and
toCollection(collectionFactory)
. They return, respectively, a set, a list, and
a programmer-specified collection type. Armed with this knowledge, we can write
a stream pipeline to extract a top-ten list from our frequency table.

Download 2,19 Mb.

Do'stlaringiz bilan baham:
1   ...   188   189   190   191   192   193   194   195   ...   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