A Beginner’ Guide — JAVA 8 Streams

So.. Should I use Streams?

Use Streams for:

// for loop
for(int i = 0; i < list.size(); i++){
if(list.get(i) < 2){
} else {
list.set(i, list.get(i) * 2);
// stream
list2 = list2.stream()
.filter(n -> n > 1)
.map(n -> n * 2)

Do not use Streams for:

Types of Stream Operations

  1. Intermediate Operations — As the name suggests, these operations are used before a terminal operation is called. They mainly serve to transform elements in the existing stream before returning a new stream.
  2. Terminal Operations —Last operation called on a stream before returning a non-stream value (void is also a potential return option).

Code Implementations

Intermediate Operations

List<Integer> list = new ArrayList<>(Arrays.asList(5, 4, 3, 2, 1));List<Integer> result = list.stream() // reads List into a stream
.filter(n -> n > 1)
.map(n -> n * 2)
// result -> [4, 6]
  1. sorted() — sorts elements according it’s natural order
  2. filter() — removes elements not satisfying the given condition
  3. map() — transforms each element into another (And in the case above: doubling the value of each element)
  4. distinct() — removes duplicate elements in stream (think sets)
  5. limit() — limits the number of elements in stream (pretty self-explanatory, I know)

Terminal Operations


  1. allMatch()— checks whether all elements in the stream fulfills the specified condition
  2. anyMatch() — checks whether at least 1 element fulfills the condition
  3. nonMatch() — opposite of allMatch
List<Integer> list = new ArrayList<>(Arrays.asList(5,4,3,2,1));// checks if all elements are smaller than 6
boolean isAllMatch = list.stream()
.allMatch(n -> n < 6);
// returns true
// checks if any elements has length > 20
List<String> words= new ArrayList<>(Arrays.asList("hello","world"));
boolean isAnyMatch = words.stream()
.anyMatch(n -> n.length > 20);
// returns false


List<Integer> list = new ArrayList<>();
Optional<Integer> findAny = list.stream().findAny();
System.out.println(findAny.isPresent()); // prints false
List<Integer> list = new ArrayList<>(Arrays.asList(5,4,3,2,1));#1
Optional<Integer> minImpl1 = list.stream()
.min((a,b) -> a.compareTo(b));
Optional<Integer> minImpl2 = list.stream()
.min((a,b) -> {
return a < b ? -1 : (a == b) ? 0 : 1
List<Integer> list = new ArrayList<>(Arrays.asList(5,4,3,2,1));Optional<Integer> reduce = list.stream()
.reduce((val, combVal) -> {
return combVal + val;
// computes sum of all values in the list


List<Integer> list = new ArrayList<>(Arrays.asList(5,4,3,2,1));// stream API implementation
list.stream().forEach(num-> System.out.print(num));
// list API implementation
list.forEach(num -> {


Useful Links:




I write sometimes because its fun

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Simple and consistent C++ Development Environment

Code Smell: Abstractions?

Easy start guide for Deepracer on Cloud in AWS:

What happens to the variable value set during the transaction, if its rollback?

My Prep Guide to a Coding Bootcamp

Fluent Logging Architecture - Fluent Bit, Fluentd & Elasticsearch

Java Libraries are Your Lambda Enemy

Your coding challenge is costing you good developers

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Darryl Leong

Darryl Leong

I write sometimes because its fun

More from Medium

Where’s Java Going In 2022?

Let’s talk about head first Java

Comparison of Java 8 vs Java 17, Should You Upgrade?

Useless Java: Day 10