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

Java8 features

  • 1.
  • 2.
     Lambdas ina nutshell  Where and how to use lambdas  The execute around pattern  Functional interfaces, type inference  Method references  Composing lambdas
  • 3.
     A lambdaexpression 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, alambda 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.
  • 6.
  • 7.
    Lambda Parameters Lambda body (Apple a1,Apple a2) a1.getWeight().compareTo(a2.getWeight()); Arrow
  • 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?
  • 10.
  • 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 isthe 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 canexecute JavaScript file directly from your Java file. In the following code, we are reading a file hello.js with the help of FileReader class.
  • 15.
  • 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
  • 18.
  • 19.
     Java providesa 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 TheComplete Reference (9th Edition), Herbert Schildt