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

2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesThousandEyes
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...panagenda
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsRavi Sanghani
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...AliaaTarek5
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationKnoldus Inc.
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Scott Andery
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfpanagenda
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 

Recently uploaded (20)

2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and Insights
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 

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