Effective Java



Download 2,19 Mb.
Pdf ko'rish
bet196/341
Sana11.07.2022
Hajmi2,19 Mb.
#776765
1   ...   192   193   194   195   196   197   198   199   ...   341
Bog'liq
Effective Java



ITEM 46: PREFER SIDE-EFFECT-FREE FUNCTIONS IN STREAMS
215
the stream as determined by the comparator. They are minor generalizations of the
min
and 
max
methods in the 
Stream
interface and are the collector analogues of
the binary operators returned by the like-named methods in 
BinaryOperator
.
Recall that we used 
BinaryOperator.maxBy
in our best-selling album example.
The final 
Collectors
method is 
joining
, which operates only on streams of
CharSequence
instances such as strings. In its parameterless form, it returns a col-
lector that simply concatenates the elements. Its one argument form takes a single
CharSequence
parameter named 
delimiter
and returns a collector that joins the
stream elements, inserting the delimiter between adjacent elements. If you pass in
a comma as the delimiter, the collector returns a comma-separated values string
(but beware that the string will be ambiguous if any of the elements in the stream
contain commas). The three argument form takes a prefix and suffix in addition to
the delimiter. The resulting collector generates strings like the ones that you get
when you print a collection, for example 
[came,
saw,
conquered]
.
In summary, the essence of programming stream pipelines is side-effect-free
function objects. This applies to all of the many function objects passed to streams
and related objects. The terminal operation 
forEach
should only be used to report
the result of a computation performed by a stream, not to perform the computa-
tion. In order to use streams properly, you have to know about collectors. The
most important collector factories are 
toList

toSet

toMap

groupingBy
, and
joining
.


CHAPTER 7
LAMBDAS AND STREAMS
216

Download 2,19 Mb.

Do'stlaringiz bilan baham:
1   ...   192   193   194   195   196   197   198   199   ...   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