DevNotes
Support
Java 8 & Streams · Chapter 22

Stream API Cheat Sheet

Every commonly used Stream method on one page — creation, intermediate and terminal operations, primitive streams, collectors and parallel streams.

◷ 4 min read§ 9 sections? 0 interview questions{ } 1 code examples

#Introduction

Java 8 Streams provide a powerful way to process collections efficiently. Below is a comprehensive list of the commonly used stream methods, categorized for easy reference.

Source
Intermediate ops (lazy)
Result

#Stream Creation Methods

MethodDescriptionExample
Stream.of(T... values)Creates a stream from multiple valuesStream.of(1, 2, 3, 4, 5);
Stream.of(array)Converts an object array into a streamStream.of(new Integer[]{1, 2, 3});
Arrays.stream(array)Creates a stream from an array (an IntStream for int[])Arrays.stream(new int[]{10, 20, 30});
collection.stream()Converts a collection into a streamList.of(1, 2, 3).stream();
Stream.generate(Supplier<T>)Creates an infinite streamStream.generate(Math::random).limit(5);
Stream.iterate(seed, UnaryOperator<T>)Generates an infinite stream using a functionStream.iterate(1, n -> n + 1).limit(10);

#Intermediate Operations

These methods transform or filter elements and return a new stream (lazily).

MethodDescriptionExample
filter(Predicate<T>)Filters elements based on a conditionlist.stream().filter(x -> x > 10);
map(Function<T, R>)Transforms elements from one type to anotherlist.stream().map(String::toUpperCase);
flatMap(Function<T, Stream<R>>)Flattens nested structures into a single streamlist.stream().flatMap(Collection::stream);
distinct()Removes duplicate elementslist.stream().distinct();
sorted()Sorts elements in natural orderlist.stream().sorted();
sorted(Comparator<T>)Sorts elements using a custom comparatorlist.stream().sorted(Comparator.reverseOrder());
peek(Consumer<T>)Performs an action on each element without changing the streamlist.stream().peek(System.out::println);
limit(long n)Returns a stream of the first n elementslist.stream().limit(5);
skip(long n)Skips the first n elementslist.stream().skip(3);

#Terminal Operations

These methods consume the stream and return a result.

MethodDescriptionExample
forEach(Consumer<T>)Performs an action for each elementlist.stream().forEach(System.out::println);
toArray()Converts the stream to an arrayInteger[] arr = list.stream().toArray(Integer[]::new);
collect(Collectors.toList())Collects elements into a ListList<Integer> result = list.stream().collect(Collectors.toList());
collect(Collectors.toSet())Collects elements into a SetSet<Integer> result = list.stream().collect(Collectors.toSet());
collect(Collectors.toMap(...))Converts elements into a Maplist.stream().collect(Collectors.toMap(x -> x, x -> x * 2));
reduce(BinaryOperator<T>)Reduces elements to a single valuelist.stream().reduce(Integer::sum);
count()Counts the elements in the streamlong count = list.stream().count();
min(Comparator<T>)Finds the minimum elementlist.stream().min(Integer::compareTo);
max(Comparator<T>)Finds the maximum elementlist.stream().max(Integer::compareTo);
findFirst()Returns the first element (if available)list.stream().findFirst();
findAny()Returns any element (useful for parallel streams)list.stream().findAny();
anyMatch(Predicate<T>)true if any element matcheslist.stream().anyMatch(x -> x > 10);
allMatch(Predicate<T>)true if all elements matchlist.stream().allMatch(x -> x > 10);
noneMatch(Predicate<T>)true if no element matcheslist.stream().noneMatch(x -> x > 10);

#Primitive Streams

Java provides specialized streams for primitive types (int, long, double) to improve performance.

Stream typeCreationExample
IntStreamIntStream.of(1, 2, 3);IntStream.range(1, 10).sum(); → 45
LongStreamLongStream.of(10L, 20L, 30L);LongStream.rangeClosed(1, 10).sum(); → 55
DoubleStreamDoubleStream.of(2.3, 3.5, 4.7);DoubleStream.generate(Math::random).limit(5);

#Collectors

Collectors transform stream results into collections or perform aggregations.

CollectorDescriptionExample
Collectors.toList()Collects elements into a Listlist.stream().collect(Collectors.toList());
Collectors.toSet()Collects elements into a Setlist.stream().collect(Collectors.toSet());
Collectors.toMap(Function, Function)Collects elements into a Maplist.stream().collect(Collectors.toMap(x -> x, x -> x * 2));
Collectors.joining(", ")Joins elements into a single stringlist.stream().map(String::valueOf).collect(Collectors.joining(", "));
Collectors.summingInt(ToIntFunction)Sums integer valueslist.stream().collect(Collectors.summingInt(x -> x));
Collectors.averagingDouble(ToDoubleFunction)Computes the averagelist.stream().collect(Collectors.averagingDouble(x -> x));
Collectors.groupingBy(Function)Groups elements into a Maplist.stream().collect(Collectors.groupingBy(x -> x % 2 == 0 ? "Even" : "Odd"));
Collectors.partitioningBy(Predicate)Partitions elements into two groupslist.stream().collect(Collectors.partitioningBy(x -> x > 5));

#Parallel Streams

Parallel streams split the workload across multiple threads.

MethodDescriptionExample
parallelStream()Creates a parallel stream from a collectionlist.parallelStream().forEach(System.out::println);
parallel()Turns an existing stream parallellist.stream().parallel();
sequential()Converts a parallel stream back to sequentiallist.parallelStream().sequential();

#Bonus Challenge — Combining Methods

Find the second longest word from a list:

Java
List<String> words = List.of("apple", "banana", "cherry", "blueberry");

String secondLongest = words.stream()
        .sorted(Comparator.comparingInt(String::length).reversed())
        .skip(1)
        .findFirst()
        .orElse("");

System.out.println(secondLongest);
Output
banana

#Summary

  • Stream creation: Stream.of(), Arrays.stream(), .stream(), generate(), iterate()
  • Transformations: filter(), map(), flatMap(), distinct(), sorted(), peek(), limit(), skip()
  • Terminal operations: forEach(), collect(), count(), reduce(), min(), max(), find…(), …Match()
  • Collectors: groupingBy(), partitioningBy(), joining(), summingInt()
  • Parallel streams: parallelStream(), parallel(), sequential()