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

Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 

Recently uploaded (20)

Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 

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