Show Menu
Cheatography

Java 8 Streams Cheat Sheet by

Specia­lized Streams

IntStream
for int elements
Double­Stream
for double elements
LongStream
for long elements
It has better perfor­mance to use these specia­lized streams when using numeric data types, because there is no boxing­/un­boxing

Suppress elements

limit
.limit(5)
will limit the result to the first 5 elements
skip
.skip(5)
will skip the first 5 elements
filter
.filter(e -> e.getS­alary() > 200000)
will keep the elements that satisfy the given predicate. In this case, all elements that have salary above 200000

Comparing elements

distinct
.dist­inct()
will compare the elements in the stream using equals() and eliminate duplicates
sorted
.sort­ed((e1, e2) -> e1.get­Nam­e().co­mpa­reT­o(e­2.g­etN­ame­()))
will sort the elements with the given compar­ator. Elements must be Compar­able.
min
Similar to sorted, but it will find the min element according to the given comparator
max
Similar to sorted, but it will find the max element according to the given comparator

Apply a function to each element

map
.map(employeeRepository::findById)
will apply the given function and substitute the elements in the stream for new elements. In this case, it received a stream of employee IDs and returned a stream of Employee objects
mapToDouble
mapToInt
mapToLong
similar to map, but the function converts the element to the specified primitive type, resulting in a specia­lized stream IntStream, Double­Stream or LongStream
flatMap
similar to map, but the number of elements resulting may be different. It's normally used to convert a List of List into a single list with all the elements
peek
.peek(e -> e.sala­ryI­ncr­eme­nt(­10.0))
will apply the give function to all elements in the list, but doesn't substitute the elements in the list
 

Reduce elements to single value

reduce
.redu­ce(0.0, Double­::sum)
will return a single value. It starts with the identity value (0.0) and applies the given function to each element in the array. In this case it's summing all elements, one by one
allMatch
.allM­atch(i -> i % 2 == 0);
will check if all elements match the given condition. If so, returns true, else returns false
anyMatch
.anyM­atch(i -> i % 2 == 0);
will check if one of the elements match the given condition. If so, returns true, else returns false
noneMatch
.none­Match(i -> i % 2 == 0);
will check if no elements match the given condition. If so, returns true, else returns false
findFirst
.find­Fir­st()
will return an Optional with the first element in the stream
forEach
forEach(e -> e.sala­ryI­ncr­eme­nt(­10.0))
will apply the given function to each element in the stream, but it's a terminal operation and returns void
count
.count()
outputs the number of elements in the stream

Collect elements

toList
colle­ct(­Col­lec­tor­s.t­oLi­st())
gather all elements in the stream into a List
toSet
colle­ct(­Col­lec­tor­s.t­oSe­t())
gather all elements in the stream into a Set
toColl­ection
colle­ct(­Col­lec­tor­s.t­oCo­lle­cti­on(­Vec­tor­::n­ew))
gather all elements in the list in an arbitrary Collection
joining
colle­ct(­Col­lec­tor­s.j­oin­ing­(", "­)).t­oS­tri­ng()
will join String elements with the given separator and return the aggregated String
summar­izi­ngD­ouble
summar­ySt­ati­stics
partit­ion­ingBy
.coll­ect­(Co­lle­cto­rs.p­ar­tit­ion­ingBy(s -> s.getG­rade() >= PASS_T­HRE­SHO­LD))
will partition the data into 2 categories based on the given condition. The result will be a Map<Bo­olean, List<S­tud­ent­>>
groupingBy
.coll­ect­(Co­lle­cto­rs.g­ro­upi­ngB­y(E­mpl­oye­e::­get­Dep­art­men­t));
will group the elements into categories based on the function. The result will be a Map<De­par­tment, List<E­mpl­oye­e>>
mapping
mappi­ng(­Per­son­::g­etL­ast­Name, toSet())
it receives a function to be applied to all elements and way of collecting downstream the elements. In this case, it will get the last name of all persons and add them to a set
reducing
       
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Selenium WebDriver Cheat Sheet Cheat Sheet
          ISTQB Test Automation Engineering Cheat Sheet