Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

A QUICK GUIDE TO

JAVA
STREAMS
LINEUP

INTRODUCTION
WHAT ARE STREAMS?
TYPES OF STREAMS
STREAM OPERATIONS
FILTERING AND MAPPING
SORTING AND REMOVING
DUPLICATES
REDUCTION AND AGGREGATION
SEARCH AND MATCH
Introduction

Java Streams, introduced in Java 8,


are a powerful and expressive
programming feature designed to
simplify the process of working with
collections, arrays, and I/O channels

They provide a functional


programming approach to processing
data, enabling developers to perform
complex operations on data sources
with concise, readable, and
maintainable code

1
What is a Stream?
In Java, a stream helps you manage
and process a sequence of data
elements more easily

It consists of connected actions that


run only when required, making it
efficient

Streams don't store or alter data, so


they're great for handling lots of
information from various sources

Java streams leverage lazy


evaluation, allowing
efficient processing of
large or even infinite data
sets by executing
operations only when
necessary and avoiding
unnecessary computations

2
Types of Streams

Sequential Streams
Sequential streams process the
elements in their original order, one at
a time. This is the default mode when
creating a stream, and it ensures that
the operations are executed in a
predictable manner

Sequential streams are


well-suited for situations
where maintaining the
order of the data is
essential or when
parallel processing
doesn't offer significant
performance benefits

3
Types of Streams
Parallel Streams
Parallel streams, on the other hand,
enable concurrent processing of the
elements by splitting the data into
multiple segments and processing
them simultaneously. This can
significantly improve performance,
particularly on multi-core systems

4
Stream Operations
Stream operations in Java are divided
into two categories: intermediate and
terminal operations

Intermediate Operations
These operations transform a stream
into a new stream without producing
a final result. They're lazy, meaning
they only execute when a terminal
operation is called. Common
intermediate operations include
filter(), map(), and sorted()

5
Stream Operations
Terminal Operations
Terminal operations produce a final
result or a side-effect, triggering the
execution of the entire stream
pipeline. Examples of terminal
operations include forEach(),
reduce(), and collect()

6
Filtering and Mapping
Filtering and mapping are common
operations in Java streams that help
manipulate and transform data
elements

filter()
The filter() method, an intermediate
operation, selects elements in a
stream based on a given condition,
using a predicate. It generates a new
stream with only the elements that
meet the condition

List<Integer> numbers
= Arrays.asList(1, 2, 3, 4, 5);
Stream<Integer> evenNumbers = numbers.stream()
.filter(n -> n % 2 == 0);

7
Filtering and Mapping

map()
The map() method is another
intermediate operation that applies a
transformation to each element in a
stream, creating a new stream with
the transformed elements. It takes a
function as an argument, which
defines how each element should be
transformed

List<String> names
= Arrays.asList("Alice", "Bob", "Charlie");
Stream<Integer> nameLengths = names.stream()
.map(name -> name.length());

8
Sorting and Removing
Duplicates
Sorting and removing duplicates
are useful operations when working
with Java streams to organize and
clean data

sorted()
The sorted() method is an
intermediate operation that sorts the
elements in a stream based on their
natural order or a provided
comparator. It creates a new stream
with the sorted elements

List<Integer> numbers
= Arrays.asList(5, 3, 1, 4, 2);
Stream<Integer> sortedNumbers = numbers.stream()
.sorted();

9
Sorting and Removing
Duplicates

distinct()
The distinct() method, another
intermediate operation, removes
duplicate elements from a stream. It
creates a new stream containing only
unique elements, based on their
natural order

List<Integer> numbersWithDuplicates =
Arrays.asList(1, 2, 2, 3, 3, 4, 4, 5);
Stream<Integer> uniqueNums = numsWithDuplicates
.stream()
.distinct();

10
Reduction and Aggregation
Reduction and aggregation operations
in Java streams help combine
elements into a single value or a new
data structure

reduce()
The reduce() method is a terminal
operation that combines stream
elements using a provided function,
often referred to as an accumulator.
It's commonly used to calculate sums,
products, or other aggregate values

List<Integer> numbers
= Arrays.asList(1, 2, 3, 4, 5);
Optional<Integer> sum = numbers.stream()
.reduce((a, b) -> a + b);

11
Reduction and Aggregation
collect()
The collect() method is another
terminal operation used to
accumulate stream elements into a
new data structure, like a list, set, or
map. It utilizes a Collector to define
the accumulation process

List<String> names
= Arrays.asList("Alice", "Bob", "Charlie");
List<String> upperCaseNames = names.stream()
.map(String::toUpperCase)
.collect(Collectors.toList());

toList(): Accumulate elements into a list


toSet(): Accumulate elements into a set
toMap(): Accumulate elements into a map
joining(): Concatenate elements into a string
groupingBy(): Group elements based on a specified
classification function
12
Search and Match
Search and match operations in Java
streams help you find elements that
satisfy certain conditions or
determine if a stream's elements meet
specific criteria

anyMatch()
The anyMatch() method is a terminal
operation that checks if any element
in a stream satisfies a given predicate.
It returns a boolean value indicating
the result

List<String> names
= Arrays.asList("Alice", "Bob", "Charlie");
boolean anyShortName = names.stream()
.anyMatch(name -> name.length() <= 3);

13
Search and Match

allMatch()

The allMatch() method is another


terminal operation that checks if all
elements in a stream meet a specified
condition. It also returns a boolean
value

List<Integer> numbers
= Arrays.asList(2, 4, 6, 8, 10);
boolean allEven = numbers.stream()
.allMatch(n -> n % 2 == 0);

14
Search and Match

findFirst()

The findFirst() method is a terminal


operation that returns the first
element in a stream that satisfies an
optional condition, wrapped in an
Optional object

List<Integer> numbers
= Arrays.asList(1, 2, 3, 4, 5);
Optional<Integer> firstEven = numbers.stream()
.filter(n -> n % 2 == 0)
.findFirst();

15
This informational document on
Java streams was brought to you
by Dileesha Rajapakse.
My goal is to provide clear,
concise, and accessible
educational resources to help you
gain a deeper understanding of
programming concepts and
technologies. I hope you found
this guide helpful and informative
as you continue your journey in
the world of Java programming.

dileeshar

You might also like