SlideShare a Scribd company logo
1 of 16
Download to read offline
JDK 8
Presented by Vladan Pulec
2
Java 8
• Estimated JDK release – early 2014 (as of April 18th)
• Currently in-progress and described features are still
subject to change
• Preview build of IDEA supports Java 8
(http://confluence.jetbrains.com/display/IDEADEV/EAP
)
• Major changes:
– Default methods in interfaces
– Lambas (anonymous functions)
– Streams
3
Default Methods in Interfaces
• Interfaces can now have default methods –
method with concrete implementations and
no state
• a lot of core JDK interfaces now have default
methods
4
Functional Interfaces
• A functional interface is an interface with only
one abstract method. For example, Runnable
interface.
• Can be declared using @FunctionalInterface
5
Lambda Expressions
• What Are they?
• Similar to anonymous classes that treat functionality as a method argument, or
code as data.
•btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
btn.setOnAction(
event -> System.out.println("Hello World!")
);
6
Syntax
1. A comma-separated list of formal parameters
2. The arrow token, ->
3. A body, which consists of a single expression or a statement block
• Note that a return statement is not an expression
• Examples:
• two inputs on the left, return block on the right:
• (int x, int y) -> { return x + y; }
• Single param with inferred type on the left: x -> x * x
• no input on the left, return on the right: () -> x
7
Lambda – Method References
(Shorthand forms)
• Static method reference:
• String::valueOf same as x ->
String.valueOf(x)
• Non-static method reference:
• Object::toString same as x ->
x.toString()
• Capturing method reference:
• x::toString same as () -> x.toString()
• Constructor reference:
• ArrayList::new same as () -> new
ArrayList<>()
8
What Lambdas Cannot Do
• Non-Final Variable Capture – variables used in
Lambdas cannot change (Lambdas are said to
be "capturing" if they access a non-static
variable or object that was defined outside of
the lambda body. )
• Control Flow – cannot break out of a loop
with an early return
• Cannot instantiate abstract classes using
lambdas
9
Fundamental shift: from imperative to functional style
• imperative style - define step by step how to
do things
• smells - mutating variables, external
iterations
• declarative styles - let the API perform the
operations
10
Commonly Useful Functional
Interfaces
• Function<T, R> - take a T as input, return an R as
ouput
• Predicate<T> - take a T as input, return a boolean
as output
• Consumer<T> - take a T as input, perform some
action and don't return anything
• Supplier<T> - with nothing as input, return a T
• BinaryOperator<T> - take two T's as input, return
one T as output, useful for "reduce" operations
11
Streams
• The new java.util.stream package provides
utilities "to support functional-style operations
on streams of values"
• similar to an iterator
• Can be sequential or running in parallel
• Two operations:
• intermediate (ie. filter or map)
• Terminal (ie. sum)
NoteL Intermediate operations are lazy, processing starts with
a terminal operation
12
Stream Operation Properties
• Stateful - A stateful operation imposes some new
property on the stream, such as uniqueness of
elements, or a maximum number of elements, or
ensuring that the elements are consumed in sorted
fashion. These are typically more expensive than
stateless intermediate operations.
• Short-circuiting - A short-circuiting operation
potentially allows processing of a stream to stop early
without examining all the elements. This is an
especially desirable property when dealing with infinite
streams; if none of the operations being invoked on a
stream are short-circuiting, then the code may never
terminate.
13
Intermediate Operations
• filter- Exclude all elements that don't match a Predicate.
• map - Perform a one-to-one transformation of elements using a Function.
• flatMap - Transform each element into zero or more elements by way of
another Stream.
• peek - Perform some action on each element as it is encountered. Primarily
useful for debugging.
• distinct - Exclude all duplicate elements according to their .equals behavior.
This is a stateful operation.
• sorted - Ensure that stream elements in subsequent operations are
encountered according to the order imposed by a Comparator. This is a stateful
operation.
• limit - Ensure that subsequent operations only see up to a maximum number
of elements. This is a stateful, short-circuiting operation.
• substream - Ensure that subsequent operations only see a range (by index) of
elements. Like String.substring except for streams. There are two forms, one
with a begin index and one with an end index as well. Both are stateful
operations, and the form with an end index is also a short-circuiting operation.
14
Terminal Operations
• forEach - Perform some action for each element in the stream.
• toArray - Dump the elements in the stream to an array.
• reduce - Combine the stream elements into one using a BinaryOperator.
• collect - Dump the elements in the stream into some container, such as a
Collection or Map.
• min - Find the minimum element of the stream according to a Comparator.
• max - Find the maximum element of the stream according to a Comparator.
• count - Find the number of elements in the stream.
• anyMatch - Find out whether at least one of the elements in the stream matches a
Predicate. This is a short-circuiting operation.
• allMatch - Find out whether every element in the stream matches a Predicate.
This is a short-circuiting operation.
• noneMatch - Find out whether zero elements in the stream match a Predicate.
This is a short-circuiting operation.
• findFirst - Find the first element in the stream. This is a short-circuiting operation.
• findAny - Find any element in the stream, which may be cheaper than findFirst for
some streams. This is a short-circuiting operation.
15
Steps in using a Stream
1. Obtain a stream from some source.
2. Perform one or more intermediate
operations.
3. Perform one terminal operation.
16
Resources
• http://www.techempower.com/blog/2013/03
/26/everything-about-java-8/
• http://zeroturnaround.com/labs/java-8-the-
first-taste-of-lambdas/#!/
• http://blog.sanaulla.info/2013/04/01/predicat
e-and-consumer-interface-in-java-util-
function-package-in-java-8/
• http://download.java.net/jdk8/docs/api/index
.html?overview-summary.html

More Related Content

What's hot

Understanding Implicits in Scala
Understanding Implicits in ScalaUnderstanding Implicits in Scala
Understanding Implicits in Scaladatamantra
 
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
 
Functional programming in Scala
Functional programming in ScalaFunctional programming in Scala
Functional programming in Scaladatamantra
 
Introduction to Reactive programming
Introduction to Reactive programmingIntroduction to Reactive programming
Introduction to Reactive programmingDwi Randy Herdinanto
 
Functional Programming in Java
Functional Programming in JavaFunctional Programming in Java
Functional Programming in JavaJim Bethancourt
 
Gatling @ Scala.Io 2013
Gatling @ Scala.Io 2013Gatling @ Scala.Io 2013
Gatling @ Scala.Io 2013slandelle
 
Galois: A System for Parallel Execution of Irregular Algorithms
Galois: A System for Parallel Execution of Irregular AlgorithmsGalois: A System for Parallel Execution of Irregular Algorithms
Galois: A System for Parallel Execution of Irregular AlgorithmsDonald Nguyen
 
Architectural Patterns - Interactive and Event Handling Patterns
Architectural Patterns  - Interactive and Event Handling PatternsArchitectural Patterns  - Interactive and Event Handling Patterns
Architectural Patterns - Interactive and Event Handling Patternsassinha
 
Apache Airflow | What Is An Operator
Apache Airflow | What Is An OperatorApache Airflow | What Is An Operator
Apache Airflow | What Is An OperatorMarc Lamberti
 
Testing Spark and Scala
Testing Spark and ScalaTesting Spark and Scala
Testing Spark and Scaladatamantra
 
Reactive programming using rx java & akka actors - pdx-scala - june 2014
Reactive programming   using rx java & akka actors - pdx-scala - june 2014Reactive programming   using rx java & akka actors - pdx-scala - june 2014
Reactive programming using rx java & akka actors - pdx-scala - june 2014Thomas Lockney
 
My Journey with Laravel by Shavkat, Ecompile.io
My Journey with Laravel by Shavkat, Ecompile.ioMy Journey with Laravel by Shavkat, Ecompile.io
My Journey with Laravel by Shavkat, Ecompile.ioappleseeds-my
 
Reactive by example - at Reversim Summit 2015
Reactive by example - at Reversim Summit 2015Reactive by example - at Reversim Summit 2015
Reactive by example - at Reversim Summit 2015Eran Harel
 
Productionalizing Spark ML
Productionalizing Spark MLProductionalizing Spark ML
Productionalizing Spark MLdatamantra
 
Pluggable Pipelines
Pluggable PipelinesPluggable Pipelines
Pluggable Pipelinessetitesuk
 
Java 8 New features
Java 8 New featuresJava 8 New features
Java 8 New featuresSon Nguyen
 

What's hot (20)

Understanding Implicits in Scala
Understanding Implicits in ScalaUnderstanding Implicits in Scala
Understanding Implicits in Scala
 
Load test REST APIs using gatling
Load test REST APIs using gatlingLoad test REST APIs using gatling
Load test REST APIs using gatling
 
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
 
Akka http 2
Akka http 2Akka http 2
Akka http 2
 
Variables in Pharo5
Variables in Pharo5Variables in Pharo5
Variables in Pharo5
 
Functional programming in Scala
Functional programming in ScalaFunctional programming in Scala
Functional programming in Scala
 
Introduction to Reactive programming
Introduction to Reactive programmingIntroduction to Reactive programming
Introduction to Reactive programming
 
Functional Programming in Java
Functional Programming in JavaFunctional Programming in Java
Functional Programming in Java
 
Gatling @ Scala.Io 2013
Gatling @ Scala.Io 2013Gatling @ Scala.Io 2013
Gatling @ Scala.Io 2013
 
Galois: A System for Parallel Execution of Irregular Algorithms
Galois: A System for Parallel Execution of Irregular AlgorithmsGalois: A System for Parallel Execution of Irregular Algorithms
Galois: A System for Parallel Execution of Irregular Algorithms
 
Architectural Patterns - Interactive and Event Handling Patterns
Architectural Patterns  - Interactive and Event Handling PatternsArchitectural Patterns  - Interactive and Event Handling Patterns
Architectural Patterns - Interactive and Event Handling Patterns
 
Apache Airflow | What Is An Operator
Apache Airflow | What Is An OperatorApache Airflow | What Is An Operator
Apache Airflow | What Is An Operator
 
Testing Spark and Scala
Testing Spark and ScalaTesting Spark and Scala
Testing Spark and Scala
 
Reactive programming using rx java & akka actors - pdx-scala - june 2014
Reactive programming   using rx java & akka actors - pdx-scala - june 2014Reactive programming   using rx java & akka actors - pdx-scala - june 2014
Reactive programming using rx java & akka actors - pdx-scala - june 2014
 
Javantura v3 - Going Reactive with RxJava – Hrvoje Crnjak
Javantura v3 - Going Reactive with RxJava – Hrvoje CrnjakJavantura v3 - Going Reactive with RxJava – Hrvoje Crnjak
Javantura v3 - Going Reactive with RxJava – Hrvoje Crnjak
 
My Journey with Laravel by Shavkat, Ecompile.io
My Journey with Laravel by Shavkat, Ecompile.ioMy Journey with Laravel by Shavkat, Ecompile.io
My Journey with Laravel by Shavkat, Ecompile.io
 
Reactive by example - at Reversim Summit 2015
Reactive by example - at Reversim Summit 2015Reactive by example - at Reversim Summit 2015
Reactive by example - at Reversim Summit 2015
 
Productionalizing Spark ML
Productionalizing Spark MLProductionalizing Spark ML
Productionalizing Spark ML
 
Pluggable Pipelines
Pluggable PipelinesPluggable Pipelines
Pluggable Pipelines
 
Java 8 New features
Java 8 New featuresJava 8 New features
Java 8 New features
 

Viewers also liked

Java Web services
Java Web servicesJava Web services
Java Web servicesvpulec
 
Ejb 2.0
Ejb 2.0Ejb 2.0
Ejb 2.0sukace
 
Internet Technology
Internet TechnologyInternet Technology
Internet Technologyhome
 
Enterprise Java Beans - EJB
Enterprise Java Beans - EJBEnterprise Java Beans - EJB
Enterprise Java Beans - EJBPeter R. Egli
 

Viewers also liked (6)

Java Web services
Java Web servicesJava Web services
Java Web services
 
TYBSc[IT]_SEM-6
TYBSc[IT]_SEM-6TYBSc[IT]_SEM-6
TYBSc[IT]_SEM-6
 
Ejb 2.0
Ejb 2.0Ejb 2.0
Ejb 2.0
 
Internet Technology
Internet TechnologyInternet Technology
Internet Technology
 
Enterprise Java Beans - EJB
Enterprise Java Beans - EJBEnterprise Java Beans - EJB
Enterprise Java Beans - EJB
 
EJB .
EJB .EJB .
EJB .
 

Similar to Java 8

Java 8 - Project Lambda
Java 8 - Project LambdaJava 8 - Project Lambda
Java 8 - Project LambdaRahman USTA
 
Stream processing from single node to a cluster
Stream processing from single node to a clusterStream processing from single node to a cluster
Stream processing from single node to a clusterGal Marder
 
JDK8 Functional API
JDK8 Functional APIJDK8 Functional API
JDK8 Functional APIJustin Lin
 
Data Pipelines with Python - NWA TechFest 2017
Data Pipelines with Python - NWA TechFest 2017Data Pipelines with Python - NWA TechFest 2017
Data Pipelines with Python - NWA TechFest 2017Casey Kinsey
 
The Road to Lambda - Mike Duigou
The Road to Lambda - Mike DuigouThe Road to Lambda - Mike Duigou
The Road to Lambda - Mike Duigoujaxconf
 
Belfast JUG 23-10-2013
Belfast JUG 23-10-2013Belfast JUG 23-10-2013
Belfast JUG 23-10-2013eamonnlong
 
Performance van Java 8 en verder - Jeroen Borgers
Performance van Java 8 en verder - Jeroen BorgersPerformance van Java 8 en verder - Jeroen Borgers
Performance van Java 8 en verder - Jeroen BorgersNLJUG
 
Java SE 8 - New Features
Java SE 8 - New FeaturesJava SE 8 - New Features
Java SE 8 - New FeaturesNaveen Hegde
 
Charles Sharp: Java 8 Streams
Charles Sharp: Java 8 StreamsCharles Sharp: Java 8 Streams
Charles Sharp: Java 8 Streamsjessitron
 
Variables Arguments and control flow_UiPath.ppt
Variables Arguments and control flow_UiPath.pptVariables Arguments and control flow_UiPath.ppt
Variables Arguments and control flow_UiPath.pptRohit Radhakrishnan
 

Similar to Java 8 (20)

Java8
Java8Java8
Java8
 
Lambda.pdf
Lambda.pdfLambda.pdf
Lambda.pdf
 
Java 8 - Project Lambda
Java 8 - Project LambdaJava 8 - Project Lambda
Java 8 - Project Lambda
 
java8
java8java8
java8
 
Stream processing from single node to a cluster
Stream processing from single node to a clusterStream processing from single node to a cluster
Stream processing from single node to a cluster
 
Lambdas in Java 8
Lambdas in Java 8Lambdas in Java 8
Lambdas in Java 8
 
Mini training - Reactive Extensions (Rx)
Mini training - Reactive Extensions (Rx)Mini training - Reactive Extensions (Rx)
Mini training - Reactive Extensions (Rx)
 
JDK8 Functional API
JDK8 Functional APIJDK8 Functional API
JDK8 Functional API
 
Data Pipelines with Python - NWA TechFest 2017
Data Pipelines with Python - NWA TechFest 2017Data Pipelines with Python - NWA TechFest 2017
Data Pipelines with Python - NWA TechFest 2017
 
The Road to Lambda - Mike Duigou
The Road to Lambda - Mike DuigouThe Road to Lambda - Mike Duigou
The Road to Lambda - Mike Duigou
 
JDK8 Streams
JDK8 StreamsJDK8 Streams
JDK8 Streams
 
Belfast JUG 23-10-2013
Belfast JUG 23-10-2013Belfast JUG 23-10-2013
Belfast JUG 23-10-2013
 
Streams in Java 8
Streams in Java 8Streams in Java 8
Streams in Java 8
 
Performance van Java 8 en verder - Jeroen Borgers
Performance van Java 8 en verder - Jeroen BorgersPerformance van Java 8 en verder - Jeroen Borgers
Performance van Java 8 en verder - Jeroen Borgers
 
Java 8 streams
Java 8 streamsJava 8 streams
Java 8 streams
 
cb streams - gavin pickin
cb streams - gavin pickincb streams - gavin pickin
cb streams - gavin pickin
 
Java SE 8 - New Features
Java SE 8 - New FeaturesJava SE 8 - New Features
Java SE 8 - New Features
 
Charles Sharp: Java 8 Streams
Charles Sharp: Java 8 StreamsCharles Sharp: Java 8 Streams
Charles Sharp: Java 8 Streams
 
Java8
Java8Java8
Java8
 
Variables Arguments and control flow_UiPath.ppt
Variables Arguments and control flow_UiPath.pptVariables Arguments and control flow_UiPath.ppt
Variables Arguments and control flow_UiPath.ppt
 

Recently uploaded

Automation Ops Series: Session 2 - Governance for UiPath projects
Automation Ops Series: Session 2 - Governance for UiPath projectsAutomation Ops Series: Session 2 - Governance for UiPath projects
Automation Ops Series: Session 2 - Governance for UiPath projectsDianaGray10
 
2024.03.12 Cost drivers of cultivated meat production.pdf
2024.03.12 Cost drivers of cultivated meat production.pdf2024.03.12 Cost drivers of cultivated meat production.pdf
2024.03.12 Cost drivers of cultivated meat production.pdfThe Good Food Institute
 
Webinar: The Art of Prioritizing Your Product Roadmap by AWS Sr PM - Tech
Webinar: The Art of Prioritizing Your Product Roadmap by AWS Sr PM - TechWebinar: The Art of Prioritizing Your Product Roadmap by AWS Sr PM - Tech
Webinar: The Art of Prioritizing Your Product Roadmap by AWS Sr PM - TechProduct School
 
20140402 - Smart house demo kit
20140402 - Smart house demo kit20140402 - Smart house demo kit
20140402 - Smart house demo kitJamie (Taka) Wang
 
The Importance of Indoor Air Quality (English)
The Importance of Indoor Air Quality (English)The Importance of Indoor Air Quality (English)
The Importance of Indoor Air Quality (English)IES VE
 
TrustArc Webinar - How to Live in a Post Third-Party Cookie World
TrustArc Webinar - How to Live in a Post Third-Party Cookie WorldTrustArc Webinar - How to Live in a Post Third-Party Cookie World
TrustArc Webinar - How to Live in a Post Third-Party Cookie WorldTrustArc
 
Scenario Library et REX Discover industry- and role- based scenarios
Scenario Library et REX Discover industry- and role- based scenariosScenario Library et REX Discover industry- and role- based scenarios
Scenario Library et REX Discover industry- and role- based scenariosErol GIRAUDY
 
Top 10 Squarespace Development Companies
Top 10 Squarespace Development CompaniesTop 10 Squarespace Development Companies
Top 10 Squarespace Development CompaniesTopCSSGallery
 
LF Energy Webinar - Unveiling OpenEEMeter 4.0
LF Energy Webinar - Unveiling OpenEEMeter 4.0LF Energy Webinar - Unveiling OpenEEMeter 4.0
LF Energy Webinar - Unveiling OpenEEMeter 4.0DanBrown980551
 
How to become a GDSC Lead GDSC MI AOE.pptx
How to become a GDSC Lead GDSC MI AOE.pptxHow to become a GDSC Lead GDSC MI AOE.pptx
How to become a GDSC Lead GDSC MI AOE.pptxKaustubhBhavsar6
 
Keep Your Finger on the Pulse of Your Building's Performance with IES Live
Keep Your Finger on the Pulse of Your Building's Performance with IES LiveKeep Your Finger on the Pulse of Your Building's Performance with IES Live
Keep Your Finger on the Pulse of Your Building's Performance with IES LiveIES VE
 
UiPath Studio Web workshop series - Day 1
UiPath Studio Web workshop series  - Day 1UiPath Studio Web workshop series  - Day 1
UiPath Studio Web workshop series - Day 1DianaGray10
 
Graphene Quantum Dots-Based Composites for Biomedical Applications
Graphene Quantum Dots-Based Composites for  Biomedical ApplicationsGraphene Quantum Dots-Based Composites for  Biomedical Applications
Graphene Quantum Dots-Based Composites for Biomedical Applicationsnooralam814309
 
Technical SEO for Improved Accessibility WTS FEST
Technical SEO for Improved Accessibility  WTS FESTTechnical SEO for Improved Accessibility  WTS FEST
Technical SEO for Improved Accessibility WTS FESTBillieHyde
 
AI Workshops at Computers In Libraries 2024
AI Workshops at Computers In Libraries 2024AI Workshops at Computers In Libraries 2024
AI Workshops at Computers In Libraries 2024Brian Pichman
 
March Patch Tuesday
March Patch TuesdayMarch Patch Tuesday
March Patch TuesdayIvanti
 
GraphSummit Copenhagen 2024 - Neo4j Vision and Roadmap.pptx
GraphSummit Copenhagen 2024 - Neo4j Vision and Roadmap.pptxGraphSummit Copenhagen 2024 - Neo4j Vision and Roadmap.pptx
GraphSummit Copenhagen 2024 - Neo4j Vision and Roadmap.pptxNeo4j
 
Introduction - IPLOOK NETWORKS CO., LTD.
Introduction - IPLOOK NETWORKS CO., LTD.Introduction - IPLOOK NETWORKS CO., LTD.
Introduction - IPLOOK NETWORKS CO., LTD.IPLOOK Networks
 
Oracle Database 23c Security New Features.pptx
Oracle Database 23c Security New Features.pptxOracle Database 23c Security New Features.pptx
Oracle Database 23c Security New Features.pptxSatishbabu Gunukula
 

Recently uploaded (20)

Automation Ops Series: Session 2 - Governance for UiPath projects
Automation Ops Series: Session 2 - Governance for UiPath projectsAutomation Ops Series: Session 2 - Governance for UiPath projects
Automation Ops Series: Session 2 - Governance for UiPath projects
 
2024.03.12 Cost drivers of cultivated meat production.pdf
2024.03.12 Cost drivers of cultivated meat production.pdf2024.03.12 Cost drivers of cultivated meat production.pdf
2024.03.12 Cost drivers of cultivated meat production.pdf
 
Webinar: The Art of Prioritizing Your Product Roadmap by AWS Sr PM - Tech
Webinar: The Art of Prioritizing Your Product Roadmap by AWS Sr PM - TechWebinar: The Art of Prioritizing Your Product Roadmap by AWS Sr PM - Tech
Webinar: The Art of Prioritizing Your Product Roadmap by AWS Sr PM - Tech
 
20140402 - Smart house demo kit
20140402 - Smart house demo kit20140402 - Smart house demo kit
20140402 - Smart house demo kit
 
The Importance of Indoor Air Quality (English)
The Importance of Indoor Air Quality (English)The Importance of Indoor Air Quality (English)
The Importance of Indoor Air Quality (English)
 
TrustArc Webinar - How to Live in a Post Third-Party Cookie World
TrustArc Webinar - How to Live in a Post Third-Party Cookie WorldTrustArc Webinar - How to Live in a Post Third-Party Cookie World
TrustArc Webinar - How to Live in a Post Third-Party Cookie World
 
Scenario Library et REX Discover industry- and role- based scenarios
Scenario Library et REX Discover industry- and role- based scenariosScenario Library et REX Discover industry- and role- based scenarios
Scenario Library et REX Discover industry- and role- based scenarios
 
Top 10 Squarespace Development Companies
Top 10 Squarespace Development CompaniesTop 10 Squarespace Development Companies
Top 10 Squarespace Development Companies
 
LF Energy Webinar - Unveiling OpenEEMeter 4.0
LF Energy Webinar - Unveiling OpenEEMeter 4.0LF Energy Webinar - Unveiling OpenEEMeter 4.0
LF Energy Webinar - Unveiling OpenEEMeter 4.0
 
How to become a GDSC Lead GDSC MI AOE.pptx
How to become a GDSC Lead GDSC MI AOE.pptxHow to become a GDSC Lead GDSC MI AOE.pptx
How to become a GDSC Lead GDSC MI AOE.pptx
 
Keep Your Finger on the Pulse of Your Building's Performance with IES Live
Keep Your Finger on the Pulse of Your Building's Performance with IES LiveKeep Your Finger on the Pulse of Your Building's Performance with IES Live
Keep Your Finger on the Pulse of Your Building's Performance with IES Live
 
SheDev 2024
SheDev 2024SheDev 2024
SheDev 2024
 
UiPath Studio Web workshop series - Day 1
UiPath Studio Web workshop series  - Day 1UiPath Studio Web workshop series  - Day 1
UiPath Studio Web workshop series - Day 1
 
Graphene Quantum Dots-Based Composites for Biomedical Applications
Graphene Quantum Dots-Based Composites for  Biomedical ApplicationsGraphene Quantum Dots-Based Composites for  Biomedical Applications
Graphene Quantum Dots-Based Composites for Biomedical Applications
 
Technical SEO for Improved Accessibility WTS FEST
Technical SEO for Improved Accessibility  WTS FESTTechnical SEO for Improved Accessibility  WTS FEST
Technical SEO for Improved Accessibility WTS FEST
 
AI Workshops at Computers In Libraries 2024
AI Workshops at Computers In Libraries 2024AI Workshops at Computers In Libraries 2024
AI Workshops at Computers In Libraries 2024
 
March Patch Tuesday
March Patch TuesdayMarch Patch Tuesday
March Patch Tuesday
 
GraphSummit Copenhagen 2024 - Neo4j Vision and Roadmap.pptx
GraphSummit Copenhagen 2024 - Neo4j Vision and Roadmap.pptxGraphSummit Copenhagen 2024 - Neo4j Vision and Roadmap.pptx
GraphSummit Copenhagen 2024 - Neo4j Vision and Roadmap.pptx
 
Introduction - IPLOOK NETWORKS CO., LTD.
Introduction - IPLOOK NETWORKS CO., LTD.Introduction - IPLOOK NETWORKS CO., LTD.
Introduction - IPLOOK NETWORKS CO., LTD.
 
Oracle Database 23c Security New Features.pptx
Oracle Database 23c Security New Features.pptxOracle Database 23c Security New Features.pptx
Oracle Database 23c Security New Features.pptx
 

Java 8

  • 1. JDK 8 Presented by Vladan Pulec
  • 2. 2 Java 8 • Estimated JDK release – early 2014 (as of April 18th) • Currently in-progress and described features are still subject to change • Preview build of IDEA supports Java 8 (http://confluence.jetbrains.com/display/IDEADEV/EAP ) • Major changes: – Default methods in interfaces – Lambas (anonymous functions) – Streams
  • 3. 3 Default Methods in Interfaces • Interfaces can now have default methods – method with concrete implementations and no state • a lot of core JDK interfaces now have default methods
  • 4. 4 Functional Interfaces • A functional interface is an interface with only one abstract method. For example, Runnable interface. • Can be declared using @FunctionalInterface
  • 5. 5 Lambda Expressions • What Are they? • Similar to anonymous classes that treat functionality as a method argument, or code as data. •btn.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { System.out.println("Hello World!"); } }); btn.setOnAction( event -> System.out.println("Hello World!") );
  • 6. 6 Syntax 1. A comma-separated list of formal parameters 2. The arrow token, -> 3. A body, which consists of a single expression or a statement block • Note that a return statement is not an expression • Examples: • two inputs on the left, return block on the right: • (int x, int y) -> { return x + y; } • Single param with inferred type on the left: x -> x * x • no input on the left, return on the right: () -> x
  • 7. 7 Lambda – Method References (Shorthand forms) • Static method reference: • String::valueOf same as x -> String.valueOf(x) • Non-static method reference: • Object::toString same as x -> x.toString() • Capturing method reference: • x::toString same as () -> x.toString() • Constructor reference: • ArrayList::new same as () -> new ArrayList<>()
  • 8. 8 What Lambdas Cannot Do • Non-Final Variable Capture – variables used in Lambdas cannot change (Lambdas are said to be "capturing" if they access a non-static variable or object that was defined outside of the lambda body. ) • Control Flow – cannot break out of a loop with an early return • Cannot instantiate abstract classes using lambdas
  • 9. 9 Fundamental shift: from imperative to functional style • imperative style - define step by step how to do things • smells - mutating variables, external iterations • declarative styles - let the API perform the operations
  • 10. 10 Commonly Useful Functional Interfaces • Function<T, R> - take a T as input, return an R as ouput • Predicate<T> - take a T as input, return a boolean as output • Consumer<T> - take a T as input, perform some action and don't return anything • Supplier<T> - with nothing as input, return a T • BinaryOperator<T> - take two T's as input, return one T as output, useful for "reduce" operations
  • 11. 11 Streams • The new java.util.stream package provides utilities "to support functional-style operations on streams of values" • similar to an iterator • Can be sequential or running in parallel • Two operations: • intermediate (ie. filter or map) • Terminal (ie. sum) NoteL Intermediate operations are lazy, processing starts with a terminal operation
  • 12. 12 Stream Operation Properties • Stateful - A stateful operation imposes some new property on the stream, such as uniqueness of elements, or a maximum number of elements, or ensuring that the elements are consumed in sorted fashion. These are typically more expensive than stateless intermediate operations. • Short-circuiting - A short-circuiting operation potentially allows processing of a stream to stop early without examining all the elements. This is an especially desirable property when dealing with infinite streams; if none of the operations being invoked on a stream are short-circuiting, then the code may never terminate.
  • 13. 13 Intermediate Operations • filter- Exclude all elements that don't match a Predicate. • map - Perform a one-to-one transformation of elements using a Function. • flatMap - Transform each element into zero or more elements by way of another Stream. • peek - Perform some action on each element as it is encountered. Primarily useful for debugging. • distinct - Exclude all duplicate elements according to their .equals behavior. This is a stateful operation. • sorted - Ensure that stream elements in subsequent operations are encountered according to the order imposed by a Comparator. This is a stateful operation. • limit - Ensure that subsequent operations only see up to a maximum number of elements. This is a stateful, short-circuiting operation. • substream - Ensure that subsequent operations only see a range (by index) of elements. Like String.substring except for streams. There are two forms, one with a begin index and one with an end index as well. Both are stateful operations, and the form with an end index is also a short-circuiting operation.
  • 14. 14 Terminal Operations • forEach - Perform some action for each element in the stream. • toArray - Dump the elements in the stream to an array. • reduce - Combine the stream elements into one using a BinaryOperator. • collect - Dump the elements in the stream into some container, such as a Collection or Map. • min - Find the minimum element of the stream according to a Comparator. • max - Find the maximum element of the stream according to a Comparator. • count - Find the number of elements in the stream. • anyMatch - Find out whether at least one of the elements in the stream matches a Predicate. This is a short-circuiting operation. • allMatch - Find out whether every element in the stream matches a Predicate. This is a short-circuiting operation. • noneMatch - Find out whether zero elements in the stream match a Predicate. This is a short-circuiting operation. • findFirst - Find the first element in the stream. This is a short-circuiting operation. • findAny - Find any element in the stream, which may be cheaper than findFirst for some streams. This is a short-circuiting operation.
  • 15. 15 Steps in using a Stream 1. Obtain a stream from some source. 2. Perform one or more intermediate operations. 3. Perform one terminal operation.
  • 16. 16 Resources • http://www.techempower.com/blog/2013/03 /26/everything-about-java-8/ • http://zeroturnaround.com/labs/java-8-the- first-taste-of-lambdas/#!/ • http://blog.sanaulla.info/2013/04/01/predicat e-and-consumer-interface-in-java-util- function-package-in-java-8/ • http://download.java.net/jdk8/docs/api/index .html?overview-summary.html