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

PPTX
Java 8 streams
ODP
Akka http
PPTX
Java.util.concurrent.concurrent hashmap
PDF
Reflection in Pharo: Beyond Smalltak
PDF
Dynamically Composing Collection Operations through Collection Promises
PDF
Understanding Reactive Programming
PPTX
Java 8 Streams And Common Operations By Harmeet Singh(Taara)
PDF
Reflection in Pharo: Beyond Smalltak
Java 8 streams
Akka http
Java.util.concurrent.concurrent hashmap
Reflection in Pharo: Beyond Smalltak
Dynamically Composing Collection Operations through Collection Promises
Understanding Reactive Programming
Java 8 Streams And Common Operations By Harmeet Singh(Taara)
Reflection in Pharo: Beyond Smalltak

What's hot (20)

PDF
Understanding Implicits in Scala
PDF
Load test REST APIs using gatling
PDF
Journey into Reactive Streams and Akka Streams
PDF
Akka http 2
PDF
Variables in Pharo5
PDF
Functional programming in Scala
PPTX
Introduction to Reactive programming
PPTX
Functional Programming in Java
PDF
Gatling @ Scala.Io 2013
PDF
Galois: A System for Parallel Execution of Irregular Algorithms
PPTX
Architectural Patterns - Interactive and Event Handling Patterns
PPTX
Apache Airflow | What Is An Operator
PDF
Testing Spark and Scala
PDF
Reactive programming using rx java & akka actors - pdx-scala - june 2014
PPTX
Javantura v3 - Going Reactive with RxJava – Hrvoje Crnjak
PDF
My Journey with Laravel by Shavkat, Ecompile.io
PDF
Reactive by example - at Reversim Summit 2015
PDF
Productionalizing Spark ML
ODP
Pluggable Pipelines
PPTX
Java 8 New features
Understanding Implicits in Scala
Load test REST APIs using gatling
Journey into Reactive Streams and Akka Streams
Akka http 2
Variables in Pharo5
Functional programming in Scala
Introduction to Reactive programming
Functional Programming in Java
Gatling @ Scala.Io 2013
Galois: A System for Parallel Execution of Irregular Algorithms
Architectural Patterns - Interactive and Event Handling Patterns
Apache Airflow | What Is An Operator
Testing Spark and Scala
Reactive programming using rx java & akka actors - pdx-scala - june 2014
Javantura v3 - Going Reactive with RxJava – Hrvoje Crnjak
My Journey with Laravel by Shavkat, Ecompile.io
Reactive by example - at Reversim Summit 2015
Productionalizing Spark ML
Pluggable Pipelines
Java 8 New features
Ad

Viewers also liked (6)

PPTX
Java Web services
PDF
TYBSc[IT]_SEM-6
PPT
Ejb 2.0
PPT
Internet Technology
PDF
Enterprise Java Beans - EJB
PPT
Java Web services
TYBSc[IT]_SEM-6
Ejb 2.0
Internet Technology
Enterprise Java Beans - EJB
Ad

Similar to Java 8 (20)

PPTX
PDF
Java Lambda internals with invoke dynamic
PDF
Apouc 2014-java-8-create-the-future
PDF
Lambda.pdf
PPTX
Java 8 Lambda and Streams
PDF
Java 8 - functional features
PDF
PPTX
Functional programming
PDF
JDK8 Functional API
PDF
Streams in Java 8
PPTX
Intro to java 8
PPTX
A brief tour of modern Java
PPTX
Java 8 Intro - Core Features
PPTX
Java 8 - An Overview
PPTX
What's New in Java 8
PPTX
Lambdas : Beyond The Basics
PPTX
A Brief Conceptual Introduction to Functional Java 8 and its API
PPTX
Java8 training - Class 1
PDF
Charles Sharp: Java 8 Streams
PPTX
Java 8 stream and c# 3.5
Java Lambda internals with invoke dynamic
Apouc 2014-java-8-create-the-future
Lambda.pdf
Java 8 Lambda and Streams
Java 8 - functional features
Functional programming
JDK8 Functional API
Streams in Java 8
Intro to java 8
A brief tour of modern Java
Java 8 Intro - Core Features
Java 8 - An Overview
What's New in Java 8
Lambdas : Beyond The Basics
A Brief Conceptual Introduction to Functional Java 8 and its API
Java8 training - Class 1
Charles Sharp: Java 8 Streams
Java 8 stream and c# 3.5

Recently uploaded (20)

PDF
Getting Started with Data Integration: FME Form 101
PDF
A comparative study of natural language inference in Swahili using monolingua...
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Encapsulation theory and applications.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Empathic Computing: Creating Shared Understanding
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Heart disease approach using modified random forest and particle swarm optimi...
PPT
Teaching material agriculture food technology
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Spectroscopy.pptx food analysis technology
Getting Started with Data Integration: FME Form 101
A comparative study of natural language inference in Swahili using monolingua...
Advanced methodologies resolving dimensionality complications for autism neur...
Network Security Unit 5.pdf for BCA BBA.
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Machine learning based COVID-19 study performance prediction
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Encapsulation theory and applications.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Empathic Computing: Creating Shared Understanding
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Heart disease approach using modified random forest and particle swarm optimi...
Teaching material agriculture food technology
Digital-Transformation-Roadmap-for-Companies.pptx
Unlocking AI with Model Context Protocol (MCP)
Building Integrated photovoltaic BIPV_UPV.pdf
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Univ-Connecticut-ChatGPT-Presentaion.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Spectroscopy.pptx food analysis technology

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