100% found this document useful (1 vote)
390 views

Java 8 Streams Cheat Sheet

The summary provides the unique surnames in uppercase of the first 15 book authors that are 50 years old or over by mapping each book to its author, filtering authors 50 years or older, collecting distinct surnames, limiting to 15, and mapping each surname to uppercase. It also computes the sum of ages of all female authors younger than 25 by mapping each book's author, filtering females under 25, mapping ages, and reducing ages to a sum.

Uploaded by

jovanlatinski
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
390 views

Java 8 Streams Cheat Sheet

The summary provides the unique surnames in uppercase of the first 15 book authors that are 50 years old or over by mapping each book to its author, filtering authors 50 years or older, collecting distinct surnames, limiting to 15, and mapping each surname to uppercase. It also computes the sum of ages of all female authors younger than 25 by mapping each book's author, filtering females under 25, mapping ages, and reducing ages to a sum.

Uploaded by

jovanlatinski
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Stream examples

Get the unique surnames in uppercase of the first 15 book


authors that are 50 years old or over.
library.stream()
.map(book -> book.getAuthor())
.filter(author -> author.getAge() >= 50)
.distinct()
.limit(15)
.map(Author::getSurname)
.map(String::toUpperCase)
.collect(toList());

Compute the sum of ages of all female authors younger than 25.
library.stream()
Intermediate operations .map(Book::getAuthor)

. Always return streams. . Lazily executed. .filter(a -> a.getGender() == Gender.FEMALE)


.map(Author::getAge)
.filter(age -> age < 25)
.reduce(0, Integer::sum):
Common examples include:

Preserves Preserves Preserves


Function
count type order Terminal operations
. Return concrete types or produce a side effect.
map . Eagerly executed.
Common examples include:
filter
Function Output When to use

distinct
reduce concrete type to cumulate elements

sorted collect list, map or set to group elements

to perform a side effect


peek forEach side effect
on elements
BROUGHT TO YOU BY

You might also like