Effective Java



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



CHAPTER 7
LAMBDAS AND STREAMS
210
Item 46: Prefer side-effect-free functions in streams 
If you’re new to streams, it can be difficult to get the hang of them. Merely
expressing your computation as a stream pipeline can be hard. When you succeed,
your program will run, but you may realize little if any benefit. Streams isn’t just
an API, it’s a paradigm based on functional programming. In order to obtain the
expressiveness, speed, and in some cases parallelizability that streams have to
offer, you have to adopt the paradigm as well as the API.
The most important part of the streams paradigm is to structure your compu-
tation as a sequence of transformations where the result of each stage is as close as
possible to a 
pure function
of the result of the previous stage. A pure function is
one whose result depends only on its input: it does not depend on any mutable
state, nor does it update any state. In order to achieve this, any function objects
that you pass into stream operations, both intermediate and terminal, should be
free of side-effects.
Occasionally, you may see streams code that looks like this snippet, which
builds a frequency table of the words in a text file:
// Uses the streams API but not the paradigm--Don't do this!
Map freq = new HashMap<>();
try (Stream words = new Scanner(file).tokens()) {
words.forEach(word -> {
freq.merge(word.toLowerCase(), 1L, Long::sum);
});
}
What’s wrong with this code? After all, it uses streams, lambdas, and method
references, and gets the right answer. Simply put, it’s not streams code at all; it’s
iterative code masquerading as streams code. It derives no benefits from the
streams API, and it’s (a bit) longer, harder to read, and less maintainable than the
corresponding iterative code. The problem stems from the fact that this code is
doing all its work in a terminal 
forEach
operation, using a lambda that mutates
external state (the frequency table). A 
forEach
operation that does anything more
than present the result of the computation performed by a stream is a “bad smell in
code,” as is a lambda that mutates state. So how should this code look?

Download 2,19 Mb.

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