Java 8 Stream Cheatsheet
Gunnar Gissel Feb 28
Originally posted at www.gunnargissel.com
Streams are a way of working with objects in a collection.
Streams allow a person to say what they want to have at the end. Contrast that with the imperative approach that requires a person to say how to collect what they want to have at the end.
Streams also allow for trivial multi-threading.
Basic Stream Methods
There are a couple basic methods that govern stream interaction. The general idea behind these methods is manipulating a collection of objects so that the only objects remaining are the ones you want.
If I had to pick the indispensable methods for working with a Stream, I'd say
Collect are on it.
Filter takes a
Predicate, and leaves only the values that the
Predicate is true for.
Map takes a
Function, and transforms the values in the stream from one type to another
Sorted takes a
Comparator, and orders the values in the stream according to the ordering specified in the
Collect stops the stream and puts the values from the stream into some kind of
Collection, like a
toArray() stops the stream and returns an array with the elements contained in the stream.
A common task, when using a collection, is to get an element out of the stream. These are generally used after
Map, to guarantee that the element you find matches some criteria and is of the preferred type.
findAny() returns an
Optional containing an element in the stream (or nothing, if the stream is empty). Order is not respected here, especially in parallel stream situations.
findFirst() returns an
Optional containing the first element in the stream (or nothing, if the stream is empty). Order is respected when using this method
max returns the maximum element of the stream for the given
min returns the minimum element of the stream for the given
Another common task is to determine if the objects meet some given criteria. These "matcher" methods indicate whether some, none or all the objects in a stream meet a given criteria.
true if all elements in the stream match the
true if any elements in the stream match the
true if no elements in the stream match the
From time to time, a stream is not quite the "right" stream. These methods create a new stream with slightly different characteristics than the old stream, whether it is a different length, different starting point, or a guarantee that only unique objects are contained.
distinct() creates a new stream that has only distinct elements (based on
.equals) in it
limit(maxSize) creates a new stream by truncating the original stream to be no longer than maxSize.
Skips the first n elements of the stream, and creates a new stream out of the rest
Creates a new stream, where the elements are sorted according to natural order
Creates a new stream, where the elements are sorted according to the
Another common task, when working with collections, is to determine some characteristics about the collection as a whole. The stream api provides a method to figure out how big a given stream is.
count() counts the number of elements in the stream
If you like this, visit my programming blog for more tips and tricks