SlideShare une entreprise Scribd logo
1  sur  20
Télécharger pour lire hors ligne
Lambda Expression
Minal Maniar
 Lambdas in a nutshell
 Where and how to use lambdas
 The execute around pattern
 Functional interfaces, type inference
 Method references
 Composing lambdas
 A lambda expression can be understood as a concise
representation of an anonymous function that can be passed
around: it doesn’t have a name, but it has a list of parameters, a
body, a return type, and also possibly a list of exceptions that can
be thrown. That’s one big definition;
let’s break it down:
 Anonymous— We say anonymous because it doesn’t have an
explicit name like a method would
normally have: less to write and think about!
 Function— We say function because a lambda isn’t associated
with a particular class like a method is.
But like a method, a lambda has a list of parameters, a body, a
return type, and a possible list of
exceptions that can be thrown.
 Passed around— A lambda expression can be passed as argument
to a method or stored in a variable.
 Concise— You don’t need to write a lot of boilerplate like you do
for anonymous classes.
 Thus, a lambda expression results in a form of
anonymous class.
 Lambda expressions are also commonly referred
to as closures.
 A functional interface is an interface that
contains one and only one abstract method.
 Thus, a functional interface typically represents
a single action.
 The standard interface Runnable is a functional
interface because it defines only one method:
run( ).
 functional interface defines the target type of a
lambda expression.
 functional interface is sometimes referred to as
a SAM type, where SAM stands for Single Abstract
Method.
tomatically
 For example, the following code in (a) can be
greatly simplified using a lambda expression
in (b) in three lines.
tomatically
Lambda
Parameters
Lambda body
(Apple a1, Apple a2) a1.getWeight().compareTo(a2.getWeight());
Arrow
Using lambda expressions, the code is shorter and cleaner. As seen in
this example, lambda expressions may have many variations. You can
handle events by defining handler classes using inner classes,
anonymous inner classes, or lambda expressions. We recommend that
you use lambda expressions because it produces a shorter, clearer, and
cleaner code.
Questions:
1. What is a lambda expression? What is the benefit of using lambda
expressions ? What is the syntax of a lambda expression?
2. What is a functional interface? Why is a functional interface required
for a lambda expression?
Oracle Nashorn
Minal Maniar
 Oracle Nashorn: A Next-Generation
JavaScript Engine for the JVM by Julien
Ponge
 Nashorn is a JavaScript engine. It is used to
execute JavaScript code dynamically at JVM
(Java Virtual Machine). Java provides a
command-line tool jjs which is used to
execute JavaScript code.
 You can execute JavaScript code by two
ways:
 Using jjs command-line tool, and
 By embedding into Java source code.
 Following is the step by step process to
execute JavaScript code at the JVM.
1. Create a file hello.js.
2. Write and save the following code into the file.
(hello.js)
3. Open terminal
4. Write command jjs hello.js and press enter.
 You can execute JavaScript file directly from
your Java file. In the following code, we are
reading a file hello.js with the help of
FileReader class.
Date/Time API
Minal Maniar
 java.util.Date class
 java.sql.Date class
 java.util.Calendar class
 java.util.GregorianCalendar class
 java.util.TimeZone class
 java.sql.Time class
 java.sql.Timestamp class
Formatting Date and Time
 java.text.DateFormat class
 java.text.SimpleDateFormat class
 java.time.LocalDate class
 java.time.LocalTime class
 java.time.LocalDateTime class
 java.time.MonthDay class
 java.time.OffsetTime class
 java.time.OffsetDateTime class
 java.time.Clock class
 java.time.ZonedDateTime class
 java.time.ZoneId class
 java.time.ZoneOffset class
 java.time.Year class
 java.time.YearMonth class
 java.time.Period class
 java.time.Duration class
 java.time.Instant class
 java.time.DayOfWeek enum
 java.time.Month enum
Stream API
Minal Maniar
 Java provides a new additional package in Java 8
called java.util.stream. This package consists of
classes, interfaces and enum to allows
functional-style operations on the elements. You
can use stream by importing java.util.stream
package.
 You can use stream to filter, collect, print, and
convert from one data structure to other etc. In
the following examples, we have apply various
operations with the help of stream.
 Here, we are filtering data by using stream. You
can see that code is optimized and maintained.
Stream provides fast execution.
Automatically
 Java The Complete Reference (9th Edition),
Herbert Schildt

Contenu connexe

Tendances

Tendances (20)

The Dark Side Of Lambda Expressions in Java 8
The Dark Side Of Lambda Expressions in Java 8The Dark Side Of Lambda Expressions in Java 8
The Dark Side Of Lambda Expressions in Java 8
 
JAVA BASICS
JAVA BASICSJAVA BASICS
JAVA BASICS
 
Python
PythonPython
Python
 
Java 8 lambda
Java 8 lambdaJava 8 lambda
Java 8 lambda
 
Invoke dynamics
Invoke dynamicsInvoke dynamics
Invoke dynamics
 
The Go Programing Language 1
The Go Programing Language 1The Go Programing Language 1
The Go Programing Language 1
 
Java basic concept
Java basic conceptJava basic concept
Java basic concept
 
Core Java Tutorial
Core Java TutorialCore Java Tutorial
Core Java Tutorial
 
Core Java introduction | Basics | free course
Core Java introduction | Basics | free course Core Java introduction | Basics | free course
Core Java introduction | Basics | free course
 
Taking User Input in Java
Taking User Input in JavaTaking User Input in Java
Taking User Input in Java
 
Lecture - 1 introduction to java
Lecture - 1 introduction to javaLecture - 1 introduction to java
Lecture - 1 introduction to java
 
Unit of competency
Unit of competencyUnit of competency
Unit of competency
 
JAVA PROGRAMMING- Exception handling - Multithreading
JAVA PROGRAMMING- Exception handling - MultithreadingJAVA PROGRAMMING- Exception handling - Multithreading
JAVA PROGRAMMING- Exception handling - Multithreading
 
CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs
CS6270 Virtual Machines - Java Virtual Machine Architecture and APIsCS6270 Virtual Machines - Java Virtual Machine Architecture and APIs
CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs
 
Core Java
Core JavaCore Java
Core Java
 
Java 101 intro to programming with java
Java 101  intro to programming with javaJava 101  intro to programming with java
Java 101 intro to programming with java
 
OCA Java SE 8 Exam Chapter 3 Core Java APIs
OCA Java SE 8 Exam Chapter 3 Core Java APIsOCA Java SE 8 Exam Chapter 3 Core Java APIs
OCA Java SE 8 Exam Chapter 3 Core Java APIs
 
Getting Program Input
Getting Program Input Getting Program Input
Getting Program Input
 
Java 8 Feature Preview
Java 8 Feature PreviewJava 8 Feature Preview
Java 8 Feature Preview
 
Java Tut1
Java Tut1Java Tut1
Java Tut1
 

En vedette (6)

3 interaction and_state_modeling
3 interaction and_state_modeling3 interaction and_state_modeling
3 interaction and_state_modeling
 
5 collection framework
5 collection framework5 collection framework
5 collection framework
 
Io
IoIo
Io
 
4 sdlc
4 sdlc4 sdlc
4 sdlc
 
2 class use case
2 class use case2 class use case
2 class use case
 
1 modeling concepts
1 modeling concepts1 modeling concepts
1 modeling concepts
 

Similaire à Java8 features

Week-1..................................
Week-1..................................Week-1..................................
Week-1..................................
kmjanani05
 

Similaire à Java8 features (20)

Lambdas, Collections Framework, Stream API
Lambdas, Collections Framework, Stream APILambdas, Collections Framework, Stream API
Lambdas, Collections Framework, Stream API
 
Functional Programming In Jdk8
Functional Programming In Jdk8 Functional Programming In Jdk8
Functional Programming In Jdk8
 
Java 8 lambdas expressions
Java 8 lambdas expressionsJava 8 lambdas expressions
Java 8 lambdas expressions
 
Functional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singhFunctional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singh
 
Lambda Expressions Java 8 Features usage
Lambda Expressions  Java 8 Features usageLambda Expressions  Java 8 Features usage
Lambda Expressions Java 8 Features usage
 
What’s new in java 8
What’s new in java 8What’s new in java 8
What’s new in java 8
 
Scala tutorial
Scala tutorialScala tutorial
Scala tutorial
 
Scala tutorial
Scala tutorialScala tutorial
Scala tutorial
 
Insight into java 1.8, OOP VS FP
Insight into java 1.8, OOP VS FPInsight into java 1.8, OOP VS FP
Insight into java 1.8, OOP VS FP
 
Java features
Java featuresJava features
Java features
 
Lambdas
LambdasLambdas
Lambdas
 
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and StreamsIntroduction of Java 8 with emphasis on Lambda Expressions and Streams
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
 
Java moderno java para Jedis episodio I
Java moderno  java para  Jedis  episodio IJava moderno  java para  Jedis  episodio I
Java moderno java para Jedis episodio I
 
Lambda expressions java8 - yousry
Lambda expressions java8 - yousryLambda expressions java8 - yousry
Lambda expressions java8 - yousry
 
Week-1..................................
Week-1..................................Week-1..................................
Week-1..................................
 
Java for Mainframers
Java for MainframersJava for Mainframers
Java for Mainframers
 
Java 8-revealed
Java 8-revealedJava 8-revealed
Java 8-revealed
 
Colloquium Report
Colloquium ReportColloquium Report
Colloquium Report
 
Eclipse and Java 8 - Eclipse Day India 2013
Eclipse and Java 8 - Eclipse Day India 2013Eclipse and Java 8 - Eclipse Day India 2013
Eclipse and Java 8 - Eclipse Day India 2013
 
Java interview questions and answers
Java interview questions and answersJava interview questions and answers
Java interview questions and answers
 

Dernier

Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Christo Ananth
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 

Dernier (20)

Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 

Java8 features

  • 2.  Lambdas in a nutshell  Where and how to use lambdas  The execute around pattern  Functional interfaces, type inference  Method references  Composing lambdas
  • 3.  A lambda expression can be understood as a concise representation of an anonymous function that can be passed around: it doesn’t have a name, but it has a list of parameters, a body, a return type, and also possibly a list of exceptions that can be thrown. That’s one big definition; let’s break it down:  Anonymous— We say anonymous because it doesn’t have an explicit name like a method would normally have: less to write and think about!  Function— We say function because a lambda isn’t associated with a particular class like a method is. But like a method, a lambda has a list of parameters, a body, a return type, and a possible list of exceptions that can be thrown.  Passed around— A lambda expression can be passed as argument to a method or stored in a variable.  Concise— You don’t need to write a lot of boilerplate like you do for anonymous classes.
  • 4.  Thus, a lambda expression results in a form of anonymous class.  Lambda expressions are also commonly referred to as closures.  A functional interface is an interface that contains one and only one abstract method.  Thus, a functional interface typically represents a single action.  The standard interface Runnable is a functional interface because it defines only one method: run( ).  functional interface defines the target type of a lambda expression.  functional interface is sometimes referred to as a SAM type, where SAM stands for Single Abstract Method.
  • 5. tomatically  For example, the following code in (a) can be greatly simplified using a lambda expression in (b) in three lines.
  • 7. Lambda Parameters Lambda body (Apple a1, Apple a2) a1.getWeight().compareTo(a2.getWeight()); Arrow
  • 8.
  • 9. Using lambda expressions, the code is shorter and cleaner. As seen in this example, lambda expressions may have many variations. You can handle events by defining handler classes using inner classes, anonymous inner classes, or lambda expressions. We recommend that you use lambda expressions because it produces a shorter, clearer, and cleaner code. Questions: 1. What is a lambda expression? What is the benefit of using lambda expressions ? What is the syntax of a lambda expression? 2. What is a functional interface? Why is a functional interface required for a lambda expression?
  • 11.  Oracle Nashorn: A Next-Generation JavaScript Engine for the JVM by Julien Ponge  Nashorn is a JavaScript engine. It is used to execute JavaScript code dynamically at JVM (Java Virtual Machine). Java provides a command-line tool jjs which is used to execute JavaScript code.  You can execute JavaScript code by two ways:  Using jjs command-line tool, and  By embedding into Java source code.
  • 12.  Following is the step by step process to execute JavaScript code at the JVM. 1. Create a file hello.js. 2. Write and save the following code into the file. (hello.js)
  • 13. 3. Open terminal 4. Write command jjs hello.js and press enter.
  • 14.  You can execute JavaScript file directly from your Java file. In the following code, we are reading a file hello.js with the help of FileReader class.
  • 16.  java.util.Date class  java.sql.Date class  java.util.Calendar class  java.util.GregorianCalendar class  java.util.TimeZone class  java.sql.Time class  java.sql.Timestamp class Formatting Date and Time  java.text.DateFormat class  java.text.SimpleDateFormat class
  • 17.  java.time.LocalDate class  java.time.LocalTime class  java.time.LocalDateTime class  java.time.MonthDay class  java.time.OffsetTime class  java.time.OffsetDateTime class  java.time.Clock class  java.time.ZonedDateTime class  java.time.ZoneId class  java.time.ZoneOffset class  java.time.Year class  java.time.YearMonth class  java.time.Period class  java.time.Duration class  java.time.Instant class  java.time.DayOfWeek enum  java.time.Month enum
  • 19.  Java provides a new additional package in Java 8 called java.util.stream. This package consists of classes, interfaces and enum to allows functional-style operations on the elements. You can use stream by importing java.util.stream package.  You can use stream to filter, collect, print, and convert from one data structure to other etc. In the following examples, we have apply various operations with the help of stream.  Here, we are filtering data by using stream. You can see that code is optimized and maintained. Stream provides fast execution.
  • 20. Automatically  Java The Complete Reference (9th Edition), Herbert Schildt