Effective Java



Download 2,19 Mb.
Pdf ko'rish
bet185/341
Sana11.07.2022
Hajmi2,19 Mb.
#776765
1   ...   181   182   183   184   185   186   187   188   ...   341
Bog'liq
Effective Java



ITEM 45: USE STREAMS JUDICIOUSLY
203
Item 45: Use streams judiciously
The streams API was added in Java 8 to ease the task of performing bulk operations,
sequentially or in parallel. This API provides two key abstractions: the 
stream
,
which represents a finite or infinite sequence of data elements, and the 
stream pipe-
line
, which represents a multistage computation on these elements. The elements in
a stream can come from anywhere. Common sources include collections, arrays,
files, regular expression pattern matchers, pseudorandom number generators, and
other streams. The data elements in a stream can be object references or primitive
values. Three primitive types are supported: 
int

long
, and 
double
.
A stream pipeline consists of a source stream followed by zero or more
intermediate operations
and one 
terminal operation
. Each intermediate operation
transforms the stream in some way, such as mapping each element to a function of
that element or filtering out all elements that do not satisfy some condition.
Intermediate operations all transform one stream into another, whose element type
may be the same as the input stream or different from it. The terminal operation
performs a final computation on the stream resulting from the last intermediate
operation, such as storing its elements into a collection, returning a certain
element, or printing all of its elements.
Stream pipelines are evaluated 
lazily
: evaluation doesn’t start until the
terminal operation is invoked, and data elements that aren’t required in order to
complete the terminal operation are never computed. This lazy evaluation is what
makes it possible to work with infinite streams. Note that a stream pipeline
without a terminal operation is a silent no-op, so don’t forget to include one.
The streams API is 
fluent
: it is designed to allow all of the calls that comprise
a pipeline to be chained into a single expression. In fact, multiple pipelines can be
chained together into a single expression. 
By default, stream pipelines run sequentially. Making a pipeline execute in
parallel is as simple as invoking the 
parallel
method on any stream in the pipe-
line, but it is seldom appropriate to do so (Item 48).
The streams API is sufficiently versatile that practically any computation can
be performed using streams, but just because you can doesn’t mean you should.
When used appropriately, streams can make programs shorter and clearer; when
used inappropriately, they can make programs difficult to read and maintain. There
are no hard and fast rules for when to use streams, but there are heuristics.
Consider the following program, which reads the words from a dictionary file
and prints all the anagram groups whose size meets a user-specified minimum.
Recall that two words are anagrams if they consist of the same letters in a different


CHAPTER 7
LAMBDAS AND STREAMS
204
order. The program reads each word from a user-specified dictionary file and
places the words into a map. The map key is the word with its letters alphabetized,
so the key for 
"staple"
is 
"aelpst"
, and the key for 
"petals"
is also 
"aelpst"
:
the two words are anagrams, and all anagrams share the same alphabetized form
(or 
alphagram
, as it is sometimes known). The map value is a list containing all of
the words that share an alphabetized form. After the dictionary has been pro-
cessed, each list is a complete anagram group. The program then iterates through
the map’s 
values()
view and prints each list whose size meets the threshold:

Download 2,19 Mb.

Do'stlaringiz bilan baham:
1   ...   181   182   183   184   185   186   187   188   ...   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