SlideShare a Scribd company logo
JAVA 8 RELEASE (MAR 18, 2014)
Outline
➢ Default Methods (Defender methods)
➢ Lambda expressions
➢ Method references
➢ Functional Interfaces
➢ Stream API (Parallel operations)
➢ Other new features
Functional Interfaces
Interfaces with only one abstract method.
With only one abstract method, these interfaces can be
easily represented with lambda expressions
Example
@FunctionalInterface
public interface SimpleFuncInterface {
public void doWork();
}
Default Methods
In Context of Support For Streams
Java 8 needed to add functionality to existing
Collection interfaces to support Streams (stream(),
forEach())
Default Methods
➢ Pre-Java 8 interfaces couldn’t have method bodies.
➢ The only way to add functionality to Interfaces was to
declare additional methods which would be
implemented in classes that implement the interface.
➢ It is impossible to add methods to an interface without
breaking the existing implementation.
Problem
Default Methods
➢ Default Methods!
➢ Java 8 allows default methods to be added to interfaces
with their full implementation
➢ Classes which implement the interface don’t have to
have implementations of the default method
➢ Allows the addition of functionality to interfaces while
preserving backward compatibility
Solution
Default Methods
public interface A {
default void foo(){
System.out.println("Calling A.foo()");
}
public class Clazz implements A {}
Clazz clazz = new Clazz();
clazz.foo(); // Calling A.foo()
Example
Lambda Expressions
The biggest new feature of Java 8 is language level support for
lambda expressions (Project Lambda).
Java lambda expressions are Java's first step into functional
programming. A Java lambda expression is thus a function
which can be created without belonging to any class.
A lambda expression can be passed around as if it was an
object and executed on demand.
Lambda Expressions
Following are the important characteristics of a lambda
expression
➢ Optional type declaration.
➢ Optional parenthesis around parameter.
➢ Optional curly braces.
➢ Optional return keyword.
Lambda Expressions
➢ With type declaration, MathOperation addition = (int a, int
b) -> a + b;
➢ Without type declaration, MathOperation subtraction = (a,
b) -> a - b;
➢ With return statement along with curly braces,
MathOperation multiplication = (int a, int b) -> { return a * b;
};
➢ Without return statement and without curly braces,
MathOperation division = (int a, int b) -> a / b;
interface MathOperation {
int operation(int a, int b);
}
Lambda Expressions
Example
Runnable task = new Runnable() {
@Override
public void run() {
System.out.println("I am a runnable task");
}
};
task.run();
// Removed boiler plate code using lambda expression
Runnable task = () -> { System.out.println("I am a runnable task");
};
task.run();
Internal vs External
Iteration
// External iteration
List<Integer> numbers = Arrays.asList(20, 40, 50, 100,
4, 3, 2, 6, 8);
for (Integer number : numbers) {
System.out.println(number);
}
// Internal iteration
numbers.forEach(number -> System.out.println(number));
➢ Predicate<T> -> test a property of the object passed as
argument
➢ Consumer<T> -> execute an action on the object
passed as argument
➢ Function<T, U> -> transform a T to a U
➢ BiFunction<T, U, V> -> transform a (T, U) to a V
➢ Supplier<T> -> provide an instance of a T (such as a
factory)
➢ UnaryOperator<T> -> a unary operator from T -> T
➢ BinaryOperator<T> -> a binary operator from (T, T) -> T
Give a look at java.util.function.*
Common JDK8
@FunctionalInterfaces
➢ You use lambda expressions to create anonymous
methods. Method references help to point to methods
by their names. A method reference is described
using :: (double colon) symbol.
➢ You can use replace Lambda Expressions with
Method References where Lambda is invoking
already defined methods.
➢ You can’t pass arguments to methods Reference.
Method references
➢ Reference to a static method
List<Integer> numbers =
Arrays.asList(1,2,3,4,5,6,7,8,9);
numbers .forEach(System .out::println);
➢ Reference to an Instance Method of a Particular Object
class Printer {
void print(Object message) {
System.out.println(message);
}
}
Printer printer = new Printer();
List<Integer> numbers = Arrays.asList(1,2,3,4,5,6,7,8,9);
numbers.forEach(printer ::print);
Method references
➢ Reference to an Instance Method of an Arbitrary Object of
a Particular Type
Integer[] numbers = {5,9,3,4,2,6,1,8,9};
Arrays.sort(numbers, Integer ::compareTo);
➢ Reference to a Constructor
interface StringToChar {
String charToString(char[] values);
}
StringtoChar strChar = String::new;
char[] values = {'J','A','V','A','8'};
System.out.println(strChar .chatToString(values));
Method references
Characteristics of Streams
➢ Streams are not related to InputStreams,
OutputStreams, etc.
➢ Streams are NOT data structures but are wrappers
around Collection that carry values from a source
through a pipeline of operations.
➢ Streams are more powerful, faster and more memory
efficient than Lists
➢ Streams are designed for lambdas
➢ Streams can easily be output as arrays or lists
➢ Streams employ lazy evaluation
➢ Streams are parallelization
➢ Streams can be “on-the-fly”
Creating Streams
➢ From individual values
Stream.of(val1, val2, …)
➢ From array
Stream.of(someArray)
Arrays.stream(someArray)
➢ From List (and other Collections)
someList.stream()
someOtherCollection.stream()
Stream Operations Pipelining
Stream Operations Pipelining
Example
List<Integer> numbers = Arrays.asList(20, 8,
200, 5, 9, 77, 67, 54, 23, 9);
numbers
.stream()
.filter(n -> n % 5 == 0)
.map(n -> n * 2)
.sorted()
.forEach(System .out::println);
Stream laziness
Intermediate & Terminal Operations
Streams – Parallelism for free
Parallelism for free
Other new features
➢ Nashorn, the new JavaScript engine
➢ Date/Time changes (java.time)
➢ Type Annotations (@Nullable, @NonEmpty,
@Readonly etc)
➢ String abc= String.join(" ", "Java", "8");
Thanks!
Any questions?

More Related Content

PPTX
User defined Functions in MATLAB Part 1
PPTX
User Defined Functions in MATLAB part 2
PPTX
Anonymous and Inline Functions in MATLAB
PPTX
User Defined Functions in MATLAB Part-4
PPTX
Matlab Programming Tips Part 1
PDF
MATLAB Programming
PPTX
Introduction to Matlab Scripts
PDF
Lambda Expressions in Java
User defined Functions in MATLAB Part 1
User Defined Functions in MATLAB part 2
Anonymous and Inline Functions in MATLAB
User Defined Functions in MATLAB Part-4
Matlab Programming Tips Part 1
MATLAB Programming
Introduction to Matlab Scripts
Lambda Expressions in Java

What's hot (20)

PDF
C standard library functions
PPTX
An Introduction to MATLAB for beginners
PPTX
Java8: what's new and what's hot
PPTX
Matlab m files and scripts
PPTX
MATLAB Scripts - Examples
PDF
What's new in java 8
PPTX
PPTX
Java chapter 3
PPT
Templates exception handling
PPT
Lecture 4
PPTX
A Brief Conceptual Introduction to Functional Java 8 and its API
PDF
Java 8 Lambda Built-in Functional Interfaces
PPTX
functions in C
PPTX
Dti2143 chapter 5
PPT
Csc1100 lecture04 ch04
PPT
User defined functions in C programmig
PDF
Programming with Lambda Expressions in Java
PPTX
Presentation on function
PPT
Loops and functions in r
PPTX
Function in C program
C standard library functions
An Introduction to MATLAB for beginners
Java8: what's new and what's hot
Matlab m files and scripts
MATLAB Scripts - Examples
What's new in java 8
Java chapter 3
Templates exception handling
Lecture 4
A Brief Conceptual Introduction to Functional Java 8 and its API
Java 8 Lambda Built-in Functional Interfaces
functions in C
Dti2143 chapter 5
Csc1100 lecture04 ch04
User defined functions in C programmig
Programming with Lambda Expressions in Java
Presentation on function
Loops and functions in r
Function in C program
Ad

Viewers also liked (20)

PPTX
Diapositivos
PDF
The Added Values and Specific Challenges of a Support Team Composed of Variou...
XLS
Grupo alberto,johana,paulo,victor,roger esan propuesta 01
PPTX
Chiesa Madre
PPT
Grupo julio,juan,eduardo,jaime,juan,julio
PPT
Exposicion2
 
PPTX
Español
DOCX
Servicio comunitario en la una yaracuy..2011 1
PDF
Final slide
DOCX
Datos Básicos de la Comunidad
PDF
Historieta clásica futuro (copia)
XLS
Grupo ana maria,sergio,victor construction sa - normal
PPTX
Bruk av office 365 i undervisning
DOCX
POLLOMETRO
DOCX
Mi primera comunión
PPT
1 p coa acem-c abreviación 2012
PPTX
Son como las weas...
PPTX
Portfolio Presentation
PDF
13 a jerrodthomas
PPTX
Definiciones
Diapositivos
The Added Values and Specific Challenges of a Support Team Composed of Variou...
Grupo alberto,johana,paulo,victor,roger esan propuesta 01
Chiesa Madre
Grupo julio,juan,eduardo,jaime,juan,julio
Exposicion2
 
Español
Servicio comunitario en la una yaracuy..2011 1
Final slide
Datos Básicos de la Comunidad
Historieta clásica futuro (copia)
Grupo ana maria,sergio,victor construction sa - normal
Bruk av office 365 i undervisning
POLLOMETRO
Mi primera comunión
1 p coa acem-c abreviación 2012
Son como las weas...
Portfolio Presentation
13 a jerrodthomas
Definiciones
Ad

Similar to Java 8 (20)

PPTX
Java 8 presentation
PPTX
java150929145120-lva1-app6892 (2).pptx
PPTX
Java 8 new features
PDF
Unit-3.pptx.pdf java api knowledge apiii
PDF
Java 8 Workshop
PDF
Java 8 features
PPTX
Java 8 Intro - Core Features
PDF
New Functional Features of Java 8
PPTX
Matlab Functions
PPSX
Esoft Metro Campus - Certificate in c / c++ programming
PPTX
Functions in C++
PPTX
Module 4_CSE3146-Advanced Java Programming-Anno_Lambda-PPTs.pptx
PPT
02basics
PDF
Java 8
PPTX
Java gets a closure
PPTX
Java8.part2
PPTX
Chp8_C++_Functions_Part2_User-defined functions.pptx
PPTX
PDF
Ds lab handouts
PPTX
c & c++ logic building concepts practice.pptx
Java 8 presentation
java150929145120-lva1-app6892 (2).pptx
Java 8 new features
Unit-3.pptx.pdf java api knowledge apiii
Java 8 Workshop
Java 8 features
Java 8 Intro - Core Features
New Functional Features of Java 8
Matlab Functions
Esoft Metro Campus - Certificate in c / c++ programming
Functions in C++
Module 4_CSE3146-Advanced Java Programming-Anno_Lambda-PPTs.pptx
02basics
Java 8
Java gets a closure
Java8.part2
Chp8_C++_Functions_Part2_User-defined functions.pptx
Ds lab handouts
c & c++ logic building concepts practice.pptx

Recently uploaded (20)

PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPT
Teaching material agriculture food technology
PDF
Electronic commerce courselecture one. Pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Approach and Philosophy of On baking technology
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Spectroscopy.pptx food analysis technology
PPTX
Big Data Technologies - Introduction.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Mobile App Security Testing_ A Comprehensive Guide.pdf
Encapsulation_ Review paper, used for researhc scholars
MYSQL Presentation for SQL database connectivity
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Teaching material agriculture food technology
Electronic commerce courselecture one. Pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Approach and Philosophy of On baking technology
Dropbox Q2 2025 Financial Results & Investor Presentation
NewMind AI Weekly Chronicles - August'25 Week I
Building Integrated photovoltaic BIPV_UPV.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
The AUB Centre for AI in Media Proposal.docx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Spectroscopy.pptx food analysis technology
Big Data Technologies - Introduction.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...

Java 8

  • 1. JAVA 8 RELEASE (MAR 18, 2014)
  • 2. Outline ➢ Default Methods (Defender methods) ➢ Lambda expressions ➢ Method references ➢ Functional Interfaces ➢ Stream API (Parallel operations) ➢ Other new features
  • 3. Functional Interfaces Interfaces with only one abstract method. With only one abstract method, these interfaces can be easily represented with lambda expressions Example @FunctionalInterface public interface SimpleFuncInterface { public void doWork(); }
  • 4. Default Methods In Context of Support For Streams Java 8 needed to add functionality to existing Collection interfaces to support Streams (stream(), forEach())
  • 5. Default Methods ➢ Pre-Java 8 interfaces couldn’t have method bodies. ➢ The only way to add functionality to Interfaces was to declare additional methods which would be implemented in classes that implement the interface. ➢ It is impossible to add methods to an interface without breaking the existing implementation. Problem
  • 6. Default Methods ➢ Default Methods! ➢ Java 8 allows default methods to be added to interfaces with their full implementation ➢ Classes which implement the interface don’t have to have implementations of the default method ➢ Allows the addition of functionality to interfaces while preserving backward compatibility Solution
  • 7. Default Methods public interface A { default void foo(){ System.out.println("Calling A.foo()"); } public class Clazz implements A {} Clazz clazz = new Clazz(); clazz.foo(); // Calling A.foo() Example
  • 8. Lambda Expressions The biggest new feature of Java 8 is language level support for lambda expressions (Project Lambda). Java lambda expressions are Java's first step into functional programming. A Java lambda expression is thus a function which can be created without belonging to any class. A lambda expression can be passed around as if it was an object and executed on demand.
  • 9. Lambda Expressions Following are the important characteristics of a lambda expression ➢ Optional type declaration. ➢ Optional parenthesis around parameter. ➢ Optional curly braces. ➢ Optional return keyword.
  • 10. Lambda Expressions ➢ With type declaration, MathOperation addition = (int a, int b) -> a + b; ➢ Without type declaration, MathOperation subtraction = (a, b) -> a - b; ➢ With return statement along with curly braces, MathOperation multiplication = (int a, int b) -> { return a * b; }; ➢ Without return statement and without curly braces, MathOperation division = (int a, int b) -> a / b; interface MathOperation { int operation(int a, int b); }
  • 11. Lambda Expressions Example Runnable task = new Runnable() { @Override public void run() { System.out.println("I am a runnable task"); } }; task.run(); // Removed boiler plate code using lambda expression Runnable task = () -> { System.out.println("I am a runnable task"); }; task.run();
  • 12. Internal vs External Iteration // External iteration List<Integer> numbers = Arrays.asList(20, 40, 50, 100, 4, 3, 2, 6, 8); for (Integer number : numbers) { System.out.println(number); } // Internal iteration numbers.forEach(number -> System.out.println(number));
  • 13. ➢ Predicate<T> -> test a property of the object passed as argument ➢ Consumer<T> -> execute an action on the object passed as argument ➢ Function<T, U> -> transform a T to a U ➢ BiFunction<T, U, V> -> transform a (T, U) to a V ➢ Supplier<T> -> provide an instance of a T (such as a factory) ➢ UnaryOperator<T> -> a unary operator from T -> T ➢ BinaryOperator<T> -> a binary operator from (T, T) -> T Give a look at java.util.function.* Common JDK8 @FunctionalInterfaces
  • 14. ➢ You use lambda expressions to create anonymous methods. Method references help to point to methods by their names. A method reference is described using :: (double colon) symbol. ➢ You can use replace Lambda Expressions with Method References where Lambda is invoking already defined methods. ➢ You can’t pass arguments to methods Reference. Method references
  • 15. ➢ Reference to a static method List<Integer> numbers = Arrays.asList(1,2,3,4,5,6,7,8,9); numbers .forEach(System .out::println); ➢ Reference to an Instance Method of a Particular Object class Printer { void print(Object message) { System.out.println(message); } } Printer printer = new Printer(); List<Integer> numbers = Arrays.asList(1,2,3,4,5,6,7,8,9); numbers.forEach(printer ::print); Method references
  • 16. ➢ Reference to an Instance Method of an Arbitrary Object of a Particular Type Integer[] numbers = {5,9,3,4,2,6,1,8,9}; Arrays.sort(numbers, Integer ::compareTo); ➢ Reference to a Constructor interface StringToChar { String charToString(char[] values); } StringtoChar strChar = String::new; char[] values = {'J','A','V','A','8'}; System.out.println(strChar .chatToString(values)); Method references
  • 17. Characteristics of Streams ➢ Streams are not related to InputStreams, OutputStreams, etc. ➢ Streams are NOT data structures but are wrappers around Collection that carry values from a source through a pipeline of operations. ➢ Streams are more powerful, faster and more memory efficient than Lists ➢ Streams are designed for lambdas ➢ Streams can easily be output as arrays or lists ➢ Streams employ lazy evaluation ➢ Streams are parallelization ➢ Streams can be “on-the-fly”
  • 18. Creating Streams ➢ From individual values Stream.of(val1, val2, …) ➢ From array Stream.of(someArray) Arrays.stream(someArray) ➢ From List (and other Collections) someList.stream() someOtherCollection.stream()
  • 20. Stream Operations Pipelining Example List<Integer> numbers = Arrays.asList(20, 8, 200, 5, 9, 77, 67, 54, 23, 9); numbers .stream() .filter(n -> n % 5 == 0) .map(n -> n * 2) .sorted() .forEach(System .out::println);
  • 21. Stream laziness Intermediate & Terminal Operations
  • 24. Other new features ➢ Nashorn, the new JavaScript engine ➢ Date/Time changes (java.time) ➢ Type Annotations (@Nullable, @NonEmpty, @Readonly etc) ➢ String abc= String.join(" ", "Java", "8");