SlideShare a Scribd company logo
1 of 10
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

What's hot

Memory allocation
Memory allocationMemory allocation
Memory allocationsanya6900
 
Functional Programming in Java
Functional Programming in JavaFunctional Programming in Java
Functional Programming in JavaJim Bethancourt
 
Storage Class Specifiers
Storage Class SpecifiersStorage Class Specifiers
Storage Class SpecifiersReddhi Basu
 
Design and Implementation of the Security Graph Language
Design and Implementation of the Security Graph LanguageDesign and Implementation of the Security Graph Language
Design and Implementation of the Security Graph LanguageAsankhaya Sharma
 
Java 8
Java 8Java 8
Java 8vpulec
 
Java 8 lambda expressions
Java 8 lambda expressionsJava 8 lambda expressions
Java 8 lambda expressionsLogan Chien
 
Mikio Braun – Data flow vs. procedural programming
Mikio Braun – Data flow vs. procedural programming Mikio Braun – Data flow vs. procedural programming
Mikio Braun – Data flow vs. procedural programming Flink Forward
 
Deploying Microservice on Docker
Deploying Microservice on DockerDeploying Microservice on Docker
Deploying Microservice on DockerKnoldus Inc.
 
Java collections the force awakens
Java collections  the force awakensJava collections  the force awakens
Java collections the force awakensRichardWarburton
 
Aggregate Programming in Scala
Aggregate Programming in ScalaAggregate Programming in Scala
Aggregate Programming in ScalaRoberto Casadei
 
Introduction to concurrent programming with Akka actors
Introduction to concurrent programming with Akka actorsIntroduction to concurrent programming with Akka actors
Introduction to concurrent programming with Akka actorsShashank L
 
The Dark Side Of Lambda Expressions in Java 8
The Dark Side Of Lambda Expressions in Java 8The Dark Side Of Lambda Expressions in Java 8
The Dark Side Of Lambda Expressions in Java 8Takipi
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1Todor Kolev
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1Todor Kolev
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1Todor Kolev
 
Re-engineering Eclipse MDT/OCL for Xtext
Re-engineering Eclipse MDT/OCL for XtextRe-engineering Eclipse MDT/OCL for Xtext
Re-engineering Eclipse MDT/OCL for XtextEdward Willink
 

What's hot (20)

Memory allocation
Memory allocationMemory allocation
Memory allocation
 
Java Programming
Java ProgrammingJava Programming
Java Programming
 
Functional Programming in Java
Functional Programming in JavaFunctional Programming in Java
Functional Programming in Java
 
Storage Class Specifiers
Storage Class SpecifiersStorage Class Specifiers
Storage Class Specifiers
 
Design and Implementation of the Security Graph Language
Design and Implementation of the Security Graph LanguageDesign and Implementation of the Security Graph Language
Design and Implementation of the Security Graph Language
 
Java 8
Java 8Java 8
Java 8
 
Java 8 lambda expressions
Java 8 lambda expressionsJava 8 lambda expressions
Java 8 lambda expressions
 
Mikio Braun – Data flow vs. procedural programming
Mikio Braun – Data flow vs. procedural programming Mikio Braun – Data flow vs. procedural programming
Mikio Braun – Data flow vs. procedural programming
 
Deploying Microservice on Docker
Deploying Microservice on DockerDeploying Microservice on Docker
Deploying Microservice on Docker
 
Link quries
Link quriesLink quries
Link quries
 
Java collections the force awakens
Java collections  the force awakensJava collections  the force awakens
Java collections the force awakens
 
Aggregate Programming in Scala
Aggregate Programming in ScalaAggregate Programming in Scala
Aggregate Programming in Scala
 
Unit 5
Unit 5Unit 5
Unit 5
 
Introduction to concurrent programming with Akka actors
Introduction to concurrent programming with Akka actorsIntroduction to concurrent programming with Akka actors
Introduction to concurrent programming with Akka actors
 
The Dark Side Of Lambda Expressions in Java 8
The Dark Side Of Lambda Expressions in Java 8The Dark Side Of Lambda Expressions in Java 8
The Dark Side Of Lambda Expressions in Java 8
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
 
Re-engineering Eclipse MDT/OCL for Xtext
Re-engineering Eclipse MDT/OCL for XtextRe-engineering Eclipse MDT/OCL for Xtext
Re-engineering Eclipse MDT/OCL for Xtext
 
Scilab vs matlab
Scilab vs matlabScilab vs matlab
Scilab vs matlab
 

Similar to Java 8 Streams API Architecture and Implementation

Functional reactive programming
Functional reactive programmingFunctional reactive programming
Functional reactive programmingAraf Karsh Hamid
 
Microservices Part 4: Functional Reactive Programming
Microservices Part 4: Functional Reactive ProgrammingMicroservices Part 4: Functional Reactive Programming
Microservices Part 4: Functional Reactive ProgrammingAraf Karsh Hamid
 
Reactive Applications with Apache Pulsar and Spring Boot
Reactive Applications with Apache Pulsar and Spring BootReactive Applications with Apache Pulsar and Spring Boot
Reactive Applications with Apache Pulsar and Spring BootVMware Tanzu
 
Lambdas : Beyond The Basics
Lambdas : Beyond The BasicsLambdas : Beyond The Basics
Lambdas : Beyond The BasicsSimon Ritter
 
Universal metrics with Apache Beam
Universal metrics with Apache BeamUniversal metrics with Apache Beam
Universal metrics with Apache BeamEtienne Chauchot
 
Java 8 - Project Lambda
Java 8 - Project LambdaJava 8 - Project Lambda
Java 8 - Project LambdaRahman USTA
 
Declarative Experimentation in Information Retrieval using PyTerrier
Declarative Experimentation in Information Retrieval using PyTerrierDeclarative Experimentation in Information Retrieval using PyTerrier
Declarative Experimentation in Information Retrieval using PyTerrierCrai Macdonald
 
Java 8 - An Overview
Java 8 - An OverviewJava 8 - An Overview
Java 8 - An OverviewIndrajit Das
 
Akka Http , Routes, Streams with Scala
Akka Http , Routes, Streams with ScalaAkka Http , Routes, Streams with Scala
Akka Http , Routes, Streams with ScalaJerry Kuru
 
Journey into Reactive Streams and Akka Streams
Journey into Reactive Streams and Akka StreamsJourney into Reactive Streams and Akka Streams
Journey into Reactive Streams and Akka StreamsKevin Webber
 

Similar to Java 8 Streams API Architecture and Implementation (20)

java8
java8java8
java8
 
Lambda.pdf
Lambda.pdfLambda.pdf
Lambda.pdf
 
Apache Crunch
Apache CrunchApache Crunch
Apache Crunch
 
Functional reactive programming
Functional reactive programmingFunctional reactive programming
Functional reactive programming
 
Microservices Part 4: Functional Reactive Programming
Microservices Part 4: Functional Reactive ProgrammingMicroservices Part 4: Functional Reactive Programming
Microservices Part 4: Functional Reactive Programming
 
Java 8
Java 8Java 8
Java 8
 
Reactive Applications with Apache Pulsar and Spring Boot
Reactive Applications with Apache Pulsar and Spring BootReactive Applications with Apache Pulsar and Spring Boot
Reactive Applications with Apache Pulsar and Spring Boot
 
Lambdas : Beyond The Basics
Lambdas : Beyond The BasicsLambdas : Beyond The Basics
Lambdas : Beyond The Basics
 
Universal metrics with Apache Beam
Universal metrics with Apache BeamUniversal metrics with Apache Beam
Universal metrics with Apache Beam
 
JDK8 Streams
JDK8 StreamsJDK8 Streams
JDK8 Streams
 
Java 8
Java 8Java 8
Java 8
 
Apache airflow
Apache airflowApache airflow
Apache airflow
 
Java 8 - Project Lambda
Java 8 - Project LambdaJava 8 - Project Lambda
Java 8 - Project Lambda
 
Declarative Experimentation in Information Retrieval using PyTerrier
Declarative Experimentation in Information Retrieval using PyTerrierDeclarative Experimentation in Information Retrieval using PyTerrier
Declarative Experimentation in Information Retrieval using PyTerrier
 
Java 8 - An Overview
Java 8 - An OverviewJava 8 - An Overview
Java 8 - An Overview
 
Smart Migration to JDK 8
Smart Migration to JDK 8Smart Migration to JDK 8
Smart Migration to JDK 8
 
Akka Http , Routes, Streams with Scala
Akka Http , Routes, Streams with ScalaAkka Http , Routes, Streams with Scala
Akka Http , Routes, Streams with Scala
 
Java 8 lambda
Java 8 lambdaJava 8 lambda
Java 8 lambda
 
Journey into Reactive Streams and Akka Streams
Journey into Reactive Streams and Akka StreamsJourney into Reactive Streams and Akka Streams
Journey into Reactive Streams and Akka Streams
 
Streams in Java 8
Streams in Java 8Streams in Java 8
Streams in Java 8
 

Recently uploaded

The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 

Recently uploaded (20)

The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 

Java 8 Streams API Architecture and Implementation

  • 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