SlideShare a Scribd company logo
Java 8 Streams
Srinivasan Raghavan
Senior Member of Technical Staff
Java Platform Group
What's all about …………………….
This presentation gives more insight how the streams api are implemented
under the hood .
This can give more insight into what library developers and app developers are
using
Design involved in Streams api
● The building block of the stream is computer science concept called pipeline .
● A pipeline is concept when the output of one of the one unit of the work is the input of the other
● Streams api use this concept to provide powerful interfaces for manipulating the data stored in
collections
● Streams api is built around functional interfaces new introduction to java 8 in java.util.function*
● Functional interfaces provides along with lambdas provide great opportunity for behavior driven
development
Functional Interface
● A functional interface can have only abstract method
● Other methods can be declared but it can default and should have a base implementation
● Lambda can be used only with functional interface
java.util.function*
Function<T, R>
Takes an input and gives an output
R apply(T t);
default funtion compose(Function<? super V, ? extends T> before)
evaluates the bef0re ands applies the result to the current
default function andThen(Function<? super R, ? extends V> after)
evaluates the current apply and input the after.apply()
More functions
BiFunction<T, U, R>
takes two inputs and gives an output
and the default fn and then evaluates bifunction and applies the output to the after
function ie after.apply()
Predicate<T>
A predicate take an input performs a test and supplies boolean and(Predicate<T>
predicate)
Returns a composed predicate that represents a short-circuiting logical AND of this
predicate and another.
negate
Returns a predicate that represents the logical negation of this predicate
or(Predicate<T> predicate)
returns or short circuiting
isEqual(Object ref)
Returns a predicate that tests if two arguments are equal according
Consumer<T>
A consumer consumes a values and return nothing nothing and then accepts the current
andthe after.accepts
Spliterator
● Spliterator Interface design is means of achieving pipelined access to the data structure
● A Split Iterator can advance through the block of data individually tryAdance() or in a bulk forEachRemaining()
and can split trySplit() into another Spliterator
● It has a series of characteristics represented by masks which indicates to the client of the spilt iterator so that
client write the behaviour according to that
● Some of these are ORDERED DISTINCT SORTED CONCURRENT IMMUTABLE
● Characteristics like order makes the code traversing conforming to order
● The split iterator also detects for concurrent modification if it does not have the characteristics CONCURRENT
and IMMUTABLE .
● A late binding Spliterator can bind to elements in collection when the first tryAdvance(), trySplit() id called.
● An non late binding can attach to the source at invocation on of any method of the split iterator
Spliterator
● As an example how a parallel computation framework, such as the {@code java.util.stream} package, would use Spliterator in a
parallel computation
● if we assume that the order of processing across subtasks doesn't matter; different (forked) tasks may further split and process
elements concurrently in undetermined order.
static class ParEach<T> extends CountedCompleter<Void> {
final Spliterator<T> spliterator;
final Consumer<T> action;
final long targetBatchSize;
ParEach(ParEach<T> parent, Spliterator<T> spliterator,
Consumer<T> action, long targetBatchSize) {
super(parent);
this.spliterator = spliterator; this.action = action;
this.targetBatchSize = targetBatchSize;
}
public void compute() {
Spliterator<T> sub;
while (spliterator.estimateSize() > targetBatchSize &&
(sub = spliterator.trySplit()) != null) {
addToPendingCount(1);
new ParEach<>(this, sub, action, targetBatchSize).fork();
}
spliterator.forEachRemaining(action);
propagateCompletion();
How the reference pipeline works?
● The basic data structure involved is a linked list of pipe stages .
● The pipe stages are initiated with a Head when the stream is initialized
● The when an ops like filter , map or reduce is added the pipe stages are added on a linked list
● the ops like filter(Predicate<? super P_OUT> predicate) map(Function<? super P_OUT, ? extends R> mapper)
flatMap(Function<? super P_OUT, ? extends Stream<? extends R>> mapper) are intermediate stages
● the terminal ops are reduce(final P_OUT identity, final BinaryOperator<P_OUT> accumulator)
,collect(Collector<? super P_OUT, A, R> collector) and forEach(Consumer<? super P_OUT> action)
● So when the terminal stages are added the code evaluates whether the ops can be parallelised and then start
the call the spliterator code and apply all the behaviours in the sequentially
Parallelisation of ops
● Each stages of the pipeline can be parallelized if the spliterator implementation and order is not required for the
execution
● So how parallel ops work . There is a method called trysplit() in spliterator which splits the data structure into two
● These two can be further split if that data is long enough and supplied the fork join common pool .
● So the fork join uses a counted completer for computing each split and compute parallel
● There is a eval parallel which evaluates the contention in the common pool before doing a fork join .
● So parallel is not guaranteed to work cause if allowed for the developer control . Careless implementation can
cause overload and less throughput

More Related Content

PPTX
Java.util.concurrent.concurrent hashmap
PPTX
A Brief Conceptual Introduction to Functional Java 8 and its API
PDF
Functional programming in Scala
PPTX
Java 8 new features
PPTX
Java 8 Streams And Common Operations By Harmeet Singh(Taara)
PDF
Understanding Implicits in Scala
ODP
Introduction to Java 8
PPTX
Java 8 stream and c# 3.5
Java.util.concurrent.concurrent hashmap
A Brief Conceptual Introduction to Functional Java 8 and its API
Functional programming in Scala
Java 8 new features
Java 8 Streams And Common Operations By Harmeet Singh(Taara)
Understanding Implicits in Scala
Introduction to Java 8
Java 8 stream and c# 3.5

What's hot (20)

PPT
Memory allocation
PPTX
Java Programming
PPTX
Functional Programming in Java
PPTX
Storage Class Specifiers
PDF
Design and Implementation of the Security Graph Language
PPTX
Java 8
PDF
Java 8 lambda expressions
PDF
Mikio Braun – Data flow vs. procedural programming
ODP
Deploying Microservice on Docker
PPTX
Link quries
PDF
Java collections the force awakens
PDF
Aggregate Programming in Scala
PDF
Introduction to concurrent programming with Akka actors
PPTX
The Dark Side Of Lambda Expressions in Java 8
PPT
Java findamentals1
PPT
Java findamentals1
PPT
Java findamentals1
PDF
Re-engineering Eclipse MDT/OCL for Xtext
PDF
Scilab vs matlab
Memory allocation
Java Programming
Functional Programming in Java
Storage Class Specifiers
Design and Implementation of the Security Graph Language
Java 8
Java 8 lambda expressions
Mikio Braun – Data flow vs. procedural programming
Deploying Microservice on Docker
Link quries
Java collections the force awakens
Aggregate Programming in Scala
Introduction to concurrent programming with Akka actors
The Dark Side Of Lambda Expressions in Java 8
Java findamentals1
Java findamentals1
Java findamentals1
Re-engineering Eclipse MDT/OCL for Xtext
Scilab vs matlab
Ad

Similar to Java 8 streams (20)

PDF
Streams in Java 8
PDF
Charles Sharp: Java 8 Streams
PPTX
JDK8 Streams
PDF
Apouc 2014-java-8-create-the-future
PDF
What is new in java 8 concurrency
PPTX
Java 8 streams
PDF
Java Lambda internals with invoke dynamic
PDF
Java 8 Stream API (Valdas Zigas)
PDF
RxJava applied [JavaDay Kyiv 2016]
PPTX
Lambdas and-streams-s ritter-v3
PPTX
Java8 and Functional Programming
PPTX
PDF
JDD 2016 - Grzegorz Rozniecki - Java 8 What Could Possibly Go Wrong
PDF
Going reactive in java
PPTX
Java8 training - Class 1
PDF
Lambda.pdf
PDF
Harnessing the Power of Java 8 Streams
PPTX
Java 8 lambda
PPTX
Reactive solutions using java 9 and spring reactor
PPTX
A brief tour of modern Java
Streams in Java 8
Charles Sharp: Java 8 Streams
JDK8 Streams
Apouc 2014-java-8-create-the-future
What is new in java 8 concurrency
Java 8 streams
Java Lambda internals with invoke dynamic
Java 8 Stream API (Valdas Zigas)
RxJava applied [JavaDay Kyiv 2016]
Lambdas and-streams-s ritter-v3
Java8 and Functional Programming
JDD 2016 - Grzegorz Rozniecki - Java 8 What Could Possibly Go Wrong
Going reactive in java
Java8 training - Class 1
Lambda.pdf
Harnessing the Power of Java 8 Streams
Java 8 lambda
Reactive solutions using java 9 and spring reactor
A brief tour of modern Java
Ad

Recently uploaded (20)

PDF
System and Network Administraation Chapter 3
PPTX
Introduction to Artificial Intelligence
PDF
Understanding Forklifts - TECH EHS Solution
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
AI in Product Development-omnex systems
PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PPTX
history of c programming in notes for students .pptx
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PPTX
Essential Infomation Tech presentation.pptx
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PPTX
ai tools demonstartion for schools and inter college
System and Network Administraation Chapter 3
Introduction to Artificial Intelligence
Understanding Forklifts - TECH EHS Solution
Odoo Companies in India – Driving Business Transformation.pdf
PTS Company Brochure 2025 (1).pdf.......
AI in Product Development-omnex systems
Softaken Excel to vCard Converter Software.pdf
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
history of c programming in notes for students .pptx
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Operating system designcfffgfgggggggvggggggggg
Internet Downloader Manager (IDM) Crack 6.42 Build 41
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Wondershare Filmora 15 Crack With Activation Key [2025
Essential Infomation Tech presentation.pptx
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
wealthsignaloriginal-com-DS-text-... (1).pdf
ai tools demonstartion for schools and inter college

Java 8 streams

  • 1. Java 8 Streams Srinivasan Raghavan Senior Member of Technical Staff Java Platform Group
  • 2. What's all about ……………………. This presentation gives more insight how the streams api are implemented under the hood . This can give more insight into what library developers and app developers are using
  • 3. Design involved in Streams api ● The building block of the stream is computer science concept called pipeline . ● A pipeline is concept when the output of one of the one unit of the work is the input of the other ● Streams api use this concept to provide powerful interfaces for manipulating the data stored in collections ● Streams api is built around functional interfaces new introduction to java 8 in java.util.function* ● Functional interfaces provides along with lambdas provide great opportunity for behavior driven development
  • 4. Functional Interface ● A functional interface can have only abstract method ● Other methods can be declared but it can default and should have a base implementation ● Lambda can be used only with functional interface
  • 5. java.util.function* Function<T, R> Takes an input and gives an output R apply(T t); default funtion compose(Function<? super V, ? extends T> before) evaluates the bef0re ands applies the result to the current default function andThen(Function<? super R, ? extends V> after) evaluates the current apply and input the after.apply()
  • 6. More functions BiFunction<T, U, R> takes two inputs and gives an output and the default fn and then evaluates bifunction and applies the output to the after function ie after.apply() Predicate<T> A predicate take an input performs a test and supplies boolean and(Predicate<T> predicate) Returns a composed predicate that represents a short-circuiting logical AND of this predicate and another. negate Returns a predicate that represents the logical negation of this predicate or(Predicate<T> predicate) returns or short circuiting isEqual(Object ref) Returns a predicate that tests if two arguments are equal according Consumer<T> A consumer consumes a values and return nothing nothing and then accepts the current andthe after.accepts
  • 7. Spliterator ● Spliterator Interface design is means of achieving pipelined access to the data structure ● A Split Iterator can advance through the block of data individually tryAdance() or in a bulk forEachRemaining() and can split trySplit() into another Spliterator ● It has a series of characteristics represented by masks which indicates to the client of the spilt iterator so that client write the behaviour according to that ● Some of these are ORDERED DISTINCT SORTED CONCURRENT IMMUTABLE ● Characteristics like order makes the code traversing conforming to order ● The split iterator also detects for concurrent modification if it does not have the characteristics CONCURRENT and IMMUTABLE . ● A late binding Spliterator can bind to elements in collection when the first tryAdvance(), trySplit() id called. ● An non late binding can attach to the source at invocation on of any method of the split iterator
  • 8. Spliterator ● As an example how a parallel computation framework, such as the {@code java.util.stream} package, would use Spliterator in a parallel computation ● if we assume that the order of processing across subtasks doesn't matter; different (forked) tasks may further split and process elements concurrently in undetermined order. static class ParEach<T> extends CountedCompleter<Void> { final Spliterator<T> spliterator; final Consumer<T> action; final long targetBatchSize; ParEach(ParEach<T> parent, Spliterator<T> spliterator, Consumer<T> action, long targetBatchSize) { super(parent); this.spliterator = spliterator; this.action = action; this.targetBatchSize = targetBatchSize; } public void compute() { Spliterator<T> sub; while (spliterator.estimateSize() > targetBatchSize && (sub = spliterator.trySplit()) != null) { addToPendingCount(1); new ParEach<>(this, sub, action, targetBatchSize).fork(); } spliterator.forEachRemaining(action); propagateCompletion();
  • 9. How the reference pipeline works? ● The basic data structure involved is a linked list of pipe stages . ● The pipe stages are initiated with a Head when the stream is initialized ● The when an ops like filter , map or reduce is added the pipe stages are added on a linked list ● the ops like filter(Predicate<? super P_OUT> predicate) map(Function<? super P_OUT, ? extends R> mapper) flatMap(Function<? super P_OUT, ? extends Stream<? extends R>> mapper) are intermediate stages ● the terminal ops are reduce(final P_OUT identity, final BinaryOperator<P_OUT> accumulator) ,collect(Collector<? super P_OUT, A, R> collector) and forEach(Consumer<? super P_OUT> action) ● So when the terminal stages are added the code evaluates whether the ops can be parallelised and then start the call the spliterator code and apply all the behaviours in the sequentially
  • 10. Parallelisation of ops ● Each stages of the pipeline can be parallelized if the spliterator implementation and order is not required for the execution ● So how parallel ops work . There is a method called trysplit() in spliterator which splits the data structure into two ● These two can be further split if that data is long enough and supplied the fork join common pool . ● So the fork join uses a counted completer for computing each split and compute parallel ● There is a eval parallel which evaluates the contention in the common pool before doing a fork join . ● So parallel is not guaranteed to work cause if allowed for the developer control . Careless implementation can cause overload and less throughput