Java Stream API for Beginners

Table of contents

The Java Stream API was introduced in Java 8. It enables functional style operations on sequences of elements like collections, arrays etc. A Stream is not a data structure as it does not store elements instead it processes data in a pipeline where
Source: Data like collections, array, I/O.
Intermediate operations: To transform stream
Terminal operations: To produce a result.
The Stream API is a part of java.util.stream.*
package. A stream is consumed only once — once you operate on a stream, it cannot be reused.
Stream Creation
A stream can be created from a data source such as a list, array or any collection.
//Create a list
List<String> list = Arrays.asList("Jui", "Jack", "Jane");
//Create a stream
Stream<String> listStream = list.stream();
//or
Stream<Integer> numStream = Stream.of(1, 2, 3, 4, 5);
Stream Methods
A Stream<T>
represents a sequence of elements and supports functional-style operations on those elements, such as:
filter(Predicate)
: It filters elements based on a condition that return boolean.
//Create a list
List<String> names = Arrays.asList("Jui", "Bob", "Ella", "Jack", "Jane", "Alice");
//To print first occuring of name that starts with letter J
Optional<String> nameStartsWithJ = names.stream() //returns Stream<String>
.filter(name -> name.startsWith("J"))
.findFirst(); //return first String ocurence
//Print first encountered name that starts with J, if none then print Not Found
System.out.println(nameStartsWithJ.orElse("Not Found")); //Jui
map(Function)
: It transforms each element
//Create a list
List<String> names = Arrays.asList("Jui", "Bob", "Ella", "Jack", "Jane", "Alice");
//Transform each element to upper case
names.stream()
.map(String::toUpperCase)
.forEach(System.out::println); //JUI BOB ELLA JACK JANE ALICE
flatMap(Function)
: It flattens a nested structure used when transformation gives collection or optional.
//Create a nested list of lists
List<List<String>> namesNested = Arrays.asList( //[[Jui, Bob, Ella], [Jack, Jane, Alice]]
Arrays.asList("Jui", "Bob", "Ella"),
Arrays.asList("Jack", "Jane", "Alice"));
List<String> flattendList = namesNested.stream()
.flatMap(List::stream)
.collect(Collectors.toList());
System.out.println(flattendList); //[Jui, Bob, Ella, Jack, Jane, Alice]
sorted()
and sorted(Comparator)
: It is used to sort elements.
//Create a list
List<String> names = Arrays.asList("Jui", "Bob", "Ella", "Jack", "Jane", "Alice");
names.stream()
.sorted(Comparator.reverseOrder()) //sorted() will return in ascending/natural order
.forEach(System.out::println); //Jui Jane Jack Ella Bob Alice
distinct()
: To remove duplicates
Stream.of(1, 3, 2, 6, 4, 2, 5, 3)
.distinct()
.forEach(System.out::println); //1 3 2 6 4 5
limit(n)
and skip(n)
Stream.of(1, 3, 2, 6, 4, 2, 5, 3)
.limit(3)
.forEach(System.out::println); //1 3 2
Stream.of(1, 3, 2, 6, 4, 2, 5, 3)
.skip(3)
.forEach(System.out::println); //6 4 2 5 3
collect()
: Coverts stream to a Collection or String.
reduce()
: Reduces elements to a single value.
Integer total = Stream.of(1, 2, 3, 4)
.reduce(0, Integer::sum);
System.out.println(total); //10
anyMatch()
, allMatch()
and noneMatch()
//Create a list
List<String> names = Arrays.asList("Jui", "Bob", "Ella", "Jack", "Jane", "Alice", "Juilee");
boolean hasJui = names.stream()
.anyMatch(name -> name.equals("Jui")); //true
boolean allJ = names.stream()
.allMatch(name -> name.startsWith("J")); //false
Summary
In this article, we have covered basic intermediate and terminal methods to perform data processing using Java Stream API.
Subscribe to my newsletter
Read articles from Juhilee Nazare directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Juhilee Nazare
Juhilee Nazare
I've 3.5+ years of experience in software development and willing to share my knowledge while working on real-time IT solutions.