SlideShare uma empresa Scribd logo
1 de 25
Baixar para ler offline
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?

Mais conteúdo relacionado

Mais procurados

C standard library functions
C standard library functionsC standard library functions
C standard library functionsVaishnavee Sharma
 
An Introduction to MATLAB for beginners
An Introduction to MATLAB for beginnersAn Introduction to MATLAB for beginners
An Introduction to MATLAB for beginnersMurshida ck
 
Java8: what's new and what's hot
Java8: what's new and what's hotJava8: what's new and what's hot
Java8: what's new and what's hotSergii Maliarov
 
Matlab m files and scripts
Matlab m files and scriptsMatlab m files and scripts
Matlab m files and scriptsAmeen San
 
What's new in java 8
What's new in java 8What's new in java 8
What's new in java 8Dian Aditya
 
Templates exception handling
Templates exception handlingTemplates exception handling
Templates exception handlingsanya6900
 
A Brief Conceptual Introduction to Functional Java 8 and its API
A Brief Conceptual Introduction to Functional Java 8 and its APIA Brief Conceptual Introduction to Functional Java 8 and its API
A Brief Conceptual Introduction to Functional Java 8 and its APIJörn Guy Süß JGS
 
Java 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional InterfacesJava 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional InterfacesGanesh Samarthyam
 
Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5alish sha
 
Csc1100 lecture04 ch04
Csc1100 lecture04 ch04Csc1100 lecture04 ch04
Csc1100 lecture04 ch04IIUM
 
User defined functions in C programmig
User defined functions in C programmigUser defined functions in C programmig
User defined functions in C programmigAppili Vamsi Krishna
 
Programming with Lambda Expressions in Java
Programming with Lambda Expressions in Java Programming with Lambda Expressions in Java
Programming with Lambda Expressions in Java langer4711
 
Presentation on function
Presentation on functionPresentation on function
Presentation on functionAbu Zaman
 
Loops and functions in r
Loops and functions in rLoops and functions in r
Loops and functions in rmanikanta361
 

Mais procurados (20)

C standard library functions
C standard library functionsC standard library functions
C standard library functions
 
An Introduction to MATLAB for beginners
An Introduction to MATLAB for beginnersAn Introduction to MATLAB for beginners
An Introduction to MATLAB for beginners
 
Java8: what's new and what's hot
Java8: what's new and what's hotJava8: what's new and what's hot
Java8: what's new and what's hot
 
Matlab m files and scripts
Matlab m files and scriptsMatlab m files and scripts
Matlab m files and scripts
 
MATLAB Scripts - Examples
MATLAB Scripts - ExamplesMATLAB Scripts - Examples
MATLAB Scripts - Examples
 
What's new in java 8
What's new in java 8What's new in java 8
What's new in java 8
 
java8
java8java8
java8
 
Java chapter 3
Java chapter 3Java chapter 3
Java chapter 3
 
Templates exception handling
Templates exception handlingTemplates exception handling
Templates exception handling
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
A Brief Conceptual Introduction to Functional Java 8 and its API
A Brief Conceptual Introduction to Functional Java 8 and its APIA Brief Conceptual Introduction to Functional Java 8 and its API
A Brief Conceptual Introduction to Functional Java 8 and its API
 
Java 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional InterfacesJava 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional Interfaces
 
functions in C
functions in Cfunctions in C
functions in C
 
Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5
 
Csc1100 lecture04 ch04
Csc1100 lecture04 ch04Csc1100 lecture04 ch04
Csc1100 lecture04 ch04
 
User defined functions in C programmig
User defined functions in C programmigUser defined functions in C programmig
User defined functions in C programmig
 
Programming with Lambda Expressions in Java
Programming with Lambda Expressions in Java Programming with Lambda Expressions in Java
Programming with Lambda Expressions in Java
 
Presentation on function
Presentation on functionPresentation on function
Presentation on function
 
Loops and functions in r
Loops and functions in rLoops and functions in r
Loops and functions in r
 
Function in C program
Function in C programFunction in C program
Function in C program
 

Destaque

The Added Values and Specific Challenges of a Support Team Composed of Variou...
The Added Values and Specific Challenges of a Support Team Composed of Variou...The Added Values and Specific Challenges of a Support Team Composed of Variou...
The Added Values and Specific Challenges of a Support Team Composed of Variou...FEANTSA
 
Grupo alberto,johana,paulo,victor,roger esan propuesta 01
Grupo alberto,johana,paulo,victor,roger esan propuesta 01Grupo alberto,johana,paulo,victor,roger esan propuesta 01
Grupo alberto,johana,paulo,victor,roger esan propuesta 01Johana Sanchez
 
Grupo julio,juan,eduardo,jaime,juan,julio
Grupo julio,juan,eduardo,jaime,juan,julioGrupo julio,juan,eduardo,jaime,juan,julio
Grupo julio,juan,eduardo,jaime,juan,julioJohana Sanchez
 
Exposicion2
Exposicion2Exposicion2
Exposicion2SEV
 
Servicio comunitario en la una yaracuy..2011 1
Servicio comunitario en la una yaracuy..2011 1Servicio comunitario en la una yaracuy..2011 1
Servicio comunitario en la una yaracuy..2011 1UNA Centro Local Yaracuy
 
Datos Básicos de la Comunidad
Datos Básicos de la ComunidadDatos Básicos de la Comunidad
Datos Básicos de la ComunidadLaura Ramirez
 
Grupo ana maria,sergio,victor construction sa - normal
Grupo ana maria,sergio,victor   construction sa - normalGrupo ana maria,sergio,victor   construction sa - normal
Grupo ana maria,sergio,victor construction sa - normalJohana Sanchez
 
Bruk av office 365 i undervisning
Bruk av office 365 i undervisningBruk av office 365 i undervisning
Bruk av office 365 i undervisningMagnus Nohr
 
Mi primera comunión
Mi primera comuniónMi primera comunión
Mi primera comuniónLaura Ramirez
 

Destaque (20)

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

Semelhante a Java 8

Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentationVan Huong
 
java150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptxjava150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptxBruceLee275640
 
New Functional Features of Java 8
New Functional Features of Java 8New Functional Features of Java 8
New Functional Features of Java 8franciscoortin
 
Matlab Functions
Matlab FunctionsMatlab Functions
Matlab FunctionsUmer Azeem
 
Esoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programmingEsoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programmingRasan Samarasinghe
 
Functions in C++
Functions in C++Functions in C++
Functions in C++home
 
Automatic Migration of Legacy Java Method Implementations to Interfaces
Automatic Migration of Legacy Java Method Implementations to InterfacesAutomatic Migration of Legacy Java Method Implementations to Interfaces
Automatic Migration of Legacy Java Method Implementations to InterfacesRaffi Khatchadourian
 
Functional Programming in Java 8
Functional Programming in Java 8Functional Programming in Java 8
Functional Programming in Java 8Omar Bashir
 

Semelhante a Java 8 (20)

Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentation
 
java150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptxjava150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptx
 
Java 8 new features
Java 8 new featuresJava 8 new features
Java 8 new features
 
Java 8 Workshop
Java 8 WorkshopJava 8 Workshop
Java 8 Workshop
 
Java 8 features
Java 8 featuresJava 8 features
Java 8 features
 
Java 8 Intro - Core Features
Java 8 Intro - Core FeaturesJava 8 Intro - Core Features
Java 8 Intro - Core Features
 
New Functional Features of Java 8
New Functional Features of Java 8New Functional Features of Java 8
New Functional Features of Java 8
 
Matlab Functions
Matlab FunctionsMatlab Functions
Matlab Functions
 
Esoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programmingEsoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programming
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
02basics
02basics02basics
02basics
 
Java 8
Java 8Java 8
Java 8
 
Java gets a closure
Java gets a closureJava gets a closure
Java gets a closure
 
Java8.part2
Java8.part2Java8.part2
Java8.part2
 
Java 8
Java 8Java 8
Java 8
 
Ds lab handouts
Ds lab handoutsDs lab handouts
Ds lab handouts
 
Automatic Migration of Legacy Java Method Implementations to Interfaces
Automatic Migration of Legacy Java Method Implementations to InterfacesAutomatic Migration of Legacy Java Method Implementations to Interfaces
Automatic Migration of Legacy Java Method Implementations to Interfaces
 
Functional Programming in Java 8
Functional Programming in Java 8Functional Programming in Java 8
Functional Programming in Java 8
 
Java8
Java8Java8
Java8
 
Colloquium Report
Colloquium ReportColloquium Report
Colloquium Report
 

Último

Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
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
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
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
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
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
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
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
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 

Último (20)

Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
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
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
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
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
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
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
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
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 

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");