100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

Java Streams API Cheat Sheet

Java Streams API Cheat Sheet

Covers creating streams, intermediate operations like filter and map, terminal operations and collectors, and safe use of parallel streams.

2 PagesIntermediateApr 2, 2026

Creating Streams

Common ways to obtain a Stream from collections and ranges.

java
Stream<String> s1 = Stream.of("a", "b", "c");Stream<Integer> s2 = List.of(1, 2, 3).stream();IntStream s3 = IntStream.range(0, 5);       // 0..4IntStream s4 = IntStream.rangeClosed(1, 5); // 1..5Stream<Integer> infinite = Stream.iterate(1, n -> n * 2).limit(5); // 1,2,4,8,16

Intermediate Operations

Lazily chained transformations that build a stream pipeline.

java
List<String> names = List.of("Alice", "Bob", "Charlie", "Dave");List<String> result = names.stream()    .filter(n -> n.length() > 3)     // keep matching elements    .map(String::toUpperCase)         // transform each element    .sorted()                         // natural ordering    .distinct()                       // remove duplicates    .limit(2)                         // take first 2    .collect(Collectors.toList());// flatMap - flattens nested streamsList<List<Integer>> nested = List.of(List.of(1, 2), List.of(3, 4));List<Integer> flat = nested.stream()    .flatMap(List::stream)    .collect(Collectors.toList()); // [1, 2, 3, 4]

Terminal Operations & Collectors

Operations that trigger the pipeline and produce a final result.

java
long count = names.stream().filter(n -> n.startsWith("A")).count();boolean any = names.stream().anyMatch(n -> n.equals("Bob"));Optional<String> first = names.stream().findFirst();int total = List.of(1, 2, 3).stream().reduce(0, Integer::sum); // reduce with identityMap<Integer, List<String>> byLength = names.stream()    .collect(Collectors.groupingBy(String::length));String joined = names.stream().collect(Collectors.joining(", ", "[", "]"));double avg = names.stream().collect(Collectors.averagingInt(String::length));

Parallel Streams

Run a stream pipeline across multiple threads for large datasets.

java
long total = List.of(1, 2, 3, 4, 5).parallelStream()    .mapToLong(Integer::longValue)    .sum();// Use only for CPU-bound, stateless, independent operations on large datasetsIntStream.rangeClosed(1, 1_000_000)    .parallel()    .filter(n -> n % 7 == 0)    .count();

Stream Fundamentals

Behaviors that trip people up the first time they use streams.

  • Stream is lazy- Intermediate operations (filter, map) build a pipeline but don't execute until a terminal operation is called
  • Streams are single-use- Calling a terminal operation consumes the stream; reusing it throws IllegalStateException
  • map vs flatMap- map transforms 1-to-1; flatMap transforms 1-to-many and flattens the result
  • Collectors.toList()/toSet()/toMap()- Common terminal collectors for materializing results
  • Method references- String::toUpperCase, System.out::println shorthand for simple lambdas
  • Primitive streams- IntStream/LongStream/DoubleStream avoid boxing overhead for numeric data
Pro Tip

Avoid mutating shared external state (like adding to an outside List) inside stream lambdas - it breaks under parallelStream() and defeats the purpose of functional-style pipelines; use collect() or reduce() instead.

Was this cheat sheet helpful?

Explore Topics

#JavaStreamsAPI#JavaStreamsAPICheatSheet#Programming#Intermediate#CreatingStreams#IntermediateOperations#TerminalOperationsCollectors#ParallelStreams#APIs#Concurrency#CommandLine#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech

Professional Web Designing Services

  • Responsive Websites
  • E-commerce Solutions
  • SEO Friendly Design
  • Fast & Secure
  • Support & Maintenance
Get a Free Quote

Share this Cheat Sheet