DEV Community

Cover image for Java 8 Stream Cheatsheet
Gunnar Gissel
Gunnar Gissel

Posted on • Originally published at gunnargissel.com

Java 8 Stream Cheatsheet

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 Map, Filter, and Collect are on it.

Filter
Filter takes a Predicate, and leaves only the values that the Predicate is true for.

Map
Map takes a Function, and transforms the values in the stream from one type to another

Sorted
Sorted takes a Comparator, and orders the values in the stream according to the ordering specified in the Comparator.

Collect
Collect stops the stream and puts the values from the stream into some kind of Collection, like a List.

toArray
toArray() stops the stream and returns an array with the elements contained in the stream.

Finders

A common task, when using a collection, is to get an element out of the stream. These are generally used after Filter and Map, to guarantee that the element you find matches some criteria and is of the preferred type.

findAny()
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()
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(Comparator comparator)
max returns the maximum element of the stream for the given Comparator

min(Comparator comparator)
min returns the minimum element of the stream for the given Comparator

Matchers

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.

allMatch(Predicate pred)
Returns true if all elements in the stream match the Predicate

anyMatch(Predicate pred)
Returns true if any elements in the stream match the Predicate

noneMatch(Predicate pred)
Returns true if no elements in the stream match the Predicate

Stream Modifiers

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()
distinct() creates a new stream that has only distinct elements (based on .equals) in it

limit(int maxSize)
limit(maxSize) creates a new stream by truncating the original stream to be no longer than maxSize.

skip(long n)
Skips the first n elements of the stream, and creates a new stream out of the rest

sorted()
Creates a new stream, where the elements are sorted according to natural order

sorted(Comparator comparator)
Creates a new stream, where the elements are sorted according to the Comparator

Stream Characterizers

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()
count() counts the number of elements in the stream

If you like this, visit my programming blog for more tips and tricks

Credits

Thank you Don LaVange for the picture of a stream

Top comments (1)

Collapse
 
joanalauren profile image
joanalauren

Hey! great post, here blog - romexsoft.com/blog/java-8-vs-java-9/ with interesting topic, hope you will like it!