0% found this document useful (0 votes)
4 views27 pages

Stream API Notes

The Stream API provides a way to operate on collections of data without storing the data itself, allowing for operations like filtering and sorting. It defines interfaces and operations that can be categorized as intermediate or terminal, with intermediate operations producing new streams and terminal operations consuming them. Additionally, the API supports parallel processing and various reduction operations to aggregate data, such as min, max, and count.

Uploaded by

Rakshaya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views27 pages

Stream API Notes

The Stream API provides a way to operate on collections of data without storing the data itself, allowing for operations like filtering and sorting. It defines interfaces and operations that can be categorized as intermediate or terminal, with intermediate operations producing new streams and terminal operations consuming them. Additionally, the API supports parallel processing and various reduction operations to aggregate data, such as min, max, and count.

Uploaded by

Rakshaya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Stream API

Stream : Overview

Operates on a data source or collection

A stream, itself never provides storage for the data

filtering, sorting or simply moves data

A stream operation by itself does not modify the data


source
Stream API

Stream API define several interfaces

java.util.stream

BaseStream extends AutoCloseable.


Stream API : BaseStream Interface

will
count
filthy
flattop

0
Stream API : Operations

Intermediate Terminal

Produces another stream Consumes a stream


Can be used to create produce a result
pipeline for seq. of actions e.g. finding min or execute
intermediate operations do some action, forEach()
not execute immediately Once a stream has been
w
specified w
action is performed when a terminal
consumed, it can’t be reused.
op is executed on new stream created by int. op
w Stream API : Intermediate Operations

Stateless Stateful

Each element is processed Processing of an element may


independently depend on aspects of the
other elements
e.g. filter()
e.g. sorted()
During parallel processing of a stream:
stateful operation may require more than one pass to
complete.
Obtaining a Stream

Collection interface has two methods: outFor Primitive type:


printin
default Stream<E> stream() system IntStream, DoubleStream
default Stream<E> parallelStream() LongStream
w void Tt
w 2
Arrays class also provide:
static <T> Stream<T> stream(T[] array)

Many T to an I/O
Stream operations return a new stream, and a stream
n
2 on a BufferedReader
can be obtained by : lines()
Question

A) [Apple, Grape, Orange, Banana]


B) [Banana, Grape, Apple, Orange]
C) [Grape, Apple, Orange, Banana]
D) [Apple, Banana, Grape, Orange]
Stream API
The forEach() method:
void forEach(Consumer<? super T> action)
void accept(T objRef)

The filter() method:


Stream<T> filter(Predicate<? super T> pred)
boolean test(T objRef)
Reduction Operations
Stream to a single value

min() or max()

Optional<T> min(Comparator<? super T> comp)

Optional<T> max(Comparator<? super T> comp)

I
Integer
Integu compare

Integer a
I
Integerb
I feared
a b T.com pae b

8
Qa
9 16

bbb
a 5
b
a compare
5
DI 5
8
3
a
Reduction Operations
Stream to a single value

min() or max()

count()

long count()
Reduction Operations
Stream to a single value

min() or max()

count()

reduce()

Optional<T> reduce(BinaryOperator<T>

can
lathe accumulator)
one

T reduce(T identityVal, BinaryOperator<T>


accumulator) T
0 a b atb
i
1 a b A b
Reduction Operations
L
T
R apply(T val, U val2)
IBi
Function
UI
A

T apply(T val, T val2) Binaryoperator ECT


It extends Bifunction
III
Reduction Operations
accumulator : AII
Stateless b e Qa btc
at
f
Non-interfering w
Associative

T reduce(T identityVal, BinaryOperator<T>


w
accumulator)
Parallel Streams
mylist
parallelStream() Collection A B c
List strings
Stream T strsl my List stream

strs2 my List parallel stream


Stream T

strsr.is Parallel
Parallel Streams
parallelStream() Collection

parallel() Stream on a parallel stream invoking


returns the
parallel
S parallel() same stream

of T
Once parallel stream is obtained the operations on the stream
can occur in parallel.
Parallel Streams
<U> U reduce(U identityVal, BiFunction<U, ? super T, U> accumulator,
BinaryOperator<U> combiner)

accumulator - function that takes two operator and apply the


operation.

combiner - function that combines two results produced by


accumulator.
Parallel Streams
Find the product of square-roots.

Is TTI 24,17151
b 7 57 58 Fo EG V7 55
IXT 57
Tredue
Stream I
Double pogroof my List parallel
n

g
Parallel Streams Fb Tax B

Find the product of square-roots.


7 18 10 24 17 5
w

a b
1718110
1 7257 se V2
57 58 am
Ri
FXMSAF.JO
Parallel Streams
Parallel to Sequential my
List parallelstreal
filter 1
EWEN S sequential()
sequential C
S unordered()
my List strawl for Each
system out printin
If data source is ordered, then stream is ordered.
To boost performance with parallel stream, use the unordered
version.
forEach() on parallel stream doesn’t preserve the order.
we w
forEachOrdered() does
Mapping intermediate
terminal
On Stream to Another Stream

<R> Stream<R> map(Function<? super T, ? extends R> mapfunc)


soooooo e

T type of element
R T Hi
R 2
result of mapping apply
Collecting
Stream to Collection

<R, A> R collect(Collector<? super T, A, R> collectorFunc)


p
R result type w
T element type
A internal accumulated type.
Collecting
Collectors
w

static <T> Collector<T, ?, List<T>> toList()


On
static <T> Collector<T, ?, Set<T>> toSet()
Collecting : ParallelStream
w

<R> R collect(Supplier<R> target, BiConsumer<R, ? super T>


accumulator, BiConsumer<R, R> combiner)
Iterators and Streams
iterator
iterator()

4s
Loop
A stay
Spliterator has Next
next
tryAdvance()

10,000
trySplit()

forEachRemaining()
1
1AM't

You might also like