SlideShare a Scribd company logo
Mohamed Ben Hassine
Java / JEE Technical Lead
Trainer
med.ingenieur@gmail.com
(+216) 52 633 220
www.linkedin.com/in/mohamedbnhassine
Java 8 Bootcamp
Java 8 Bootcamp
What you will Learn
• Get Started
• Lambda Expressions
• Java 8 Stream API
• New Date and Time API
• Nashorn: New JavaScript Engine
• Miscellaneous
LAMBDA EXPRESSION
History of Java releases
4
1995
JDKIntroduction
January1996
JDK1.0
“OAK”JDK1.0
December1998
J2SE1.2
FoundationofJCJ
Swing,Collections
February2002
J2SE1.4
Assert,JAXP,IPv6
RegularExpressions
February 1997
JDK 1.1
Java Beans, RMI , JDBC,
JIT
May 2000
J2SE 1.3
HotSpot JVM , JNDI,
Java Debugger
September 2004
J2SE 5.0
Generics, Metadata,
Concurrency, Static
Import
September 2006
Java SE 6
Java Compiler API
,Support JDBC 4.0 , JVM
improvements
July 2011
Java SE 7
Try-with-Resources
Strings in switch
New IO API , Dynamic
languages
March 2015
Java SE 8
Project Lambda Streams
API , Functional Interfaces
,Nashorn Engine ,New
Date Time API ,
Annotations on Type
2017
Java SE 9
Jshell Java REPL ,
Support HTTP 2.0 , G1
default Garbage
Collector
Get Started
Why Java 8 Now ?
5
Get Started
Revolution in Processing power
Parallel multicores architectures
Big Data Applications
Code behavior
Code as data
Functional programming Support
Enhance core librairies
Interface unlocking (concrete methods )
Backward compatibility
Java / Javascript
JavaScript made great in Java 8
Install and configure Java 8
6
Get Started
Project Based Course
7
Get Started
Employee management
Throughout the whole course , we will develop a java
console app which offers suck key features :
 Add manage employee
 Manage employees personal details , salaries and bonus
 Search employee by predefined criteria
Lambda Expressions
First Lambda Expression
Comparator<Employee> compLambda = ( emp1, emp2) -> {
int i = Integer.compare(emp1.getAge(), emp2.getAge());
if (i != 0) {
return i;
} else {
i = Double.compare(emp1.getSalary(), emp2.getSalary());
if (i != 0)
return i;
}
return i;
};
Collections.sort(persons, compLambda);
Much Easier !
persons.sort(Comparator.comparing(Employee::getAge).thenComparing(Employee::getSalary));
Don’t worry about this code for now. This
Course will explain what it does and how you can develop
similar code!
Lambda Expressions
Why Lambda Expression
 Using anonymous classes for code behavior parameterization is verbose
 Programmers struggle with anonymous classes.
(confusion “this”, no access to non-final local variables)
 Convenient for new streams library
persons.forEach(emp -> emp.setSalary(3000));
Solution :
Lambda let you represent a behavior or pass code in a concise way.
Another way to write anonymous classes
Comparator<Employee> compLambda = ( emp1, emp2) -> { return emp1.getJobTitle().compareTo(emp2.getJobTitle()); }
Lambda
Parameters
Arrow
Lambda Body
Lambda Expressions
How to Write Lambda Expression
The simple way
More than One line of code
Runnable r = () -> {
for (int i = 0; i < 2016; i++) {
System.out.println("Hello to your Java SE 8 Bootcamp 2016 !");
}
};
More than One Argument
Comparator<Employee> compLambda = (Employee emp1, Employee emp2) -> {
return emp1.getJobTitle().compareTo(emp2.getJobTitle());
}
;
FileFilter filter = (File file) -> file.getName().endsWith("class");
Lambda Expressions
So what is exactly Lambda Expression ?
Everything in Java has a type so what for Lambda ? :
Lambda Expression is Functional Interface ( new Java 8 concept , Interface with one
abstract Method..example Runnable , Comparator interfaces known prior to Java
SE8 as Single Abstract Method Interface).
Where to use A Lambda Expression ?
In each case it could be assigned to a variable of Type Functional Interface
Runnable r = () -> System.out.println("Hello to your Java SE 8 Bootcamp 2016 !");
;
Lambda Expression
Lambda Expressions
Functional Interface
An Interface with only one abstract method
Example :
public interface Runnable {
public abstract void run();
}
@FunctionalInterface
public interface SimpleInterface {
public void doSomeJob() ;
}
Define Java SE8 Functional Interface
Lambda Expressions
Predefined Functional Interface in JAVA SE8
New Java Package : java.util.function
Divided into 4 categories :
Supplier Consumer Predicate
/** @since 1.8
*/
@FunctionalInterface
public interface
Supplier<T> {
/**
* Gets a result.
*
* @return a result
*/
T get();
}
Function
/** @since 1.8
*/
@FunctionalInterface
public interface Consumer<T>
{
/**
* @param t the input
argument
*/
void accept(T t);
}
/** @since 1.8
*/
@FunctionalInterface
public interface
Predicate<T> {
/**
* Evaluates this
predicate on the given
argument.
*/
boolean test(T t);
}
/** @since 1.8
*/
@FunctionalInterface
public interface Function<T,
R> {
/**
* Applies this function
to the given argument.
*/
R apply(T t);
}
Lambda Expressions
Method Reference
Simply : An other way to write Lambda Expression
Comparator<Employee> compLambda = (Employee emp1, Employee emp2) -> {
return Double.compare(emp1.getSalary(), emp2.getSalary()) ;
}
;
Comparator<Employee> compLambdaRef = Comparator.comparing(Employee::getSalary);
– Replace
(args) -> ClassName.staticMethodName(args)
– with
ClassName::staticMethodName
Lambda Expressions
So how to process Data with this Lambda ?
List<Employee> persons = new ArrayList<>();
persons.add(new Employee("Mohamed", 30, 2500, new Date(), true, "Java / JEE TL"));
persons.add(new Employee("Ferdaous", 24, 1900, new Date(), false, "IT Consultant"));
// Prior to Java SE 8
for (Employee employee : persons) { System.out.println(employee) ;}
Using Lambda Expression
// Traverse collection with Lambda Expression
persons.forEach(emp -> System.out.println(emp));
// More concise syntax with Method Reference
persons.forEach(System.out::println);
Where does this method come from ? All interface
implementations of Iterable should be refactored !..
Response Next episode ..
Lambda Expressions
Default Method
In Java 8 a method can be implemented in an interface.
 New Java 8 Concept for backward compatibility
Allow to add methods to an existing interface long after it has been released
without breaking the interface
Interface
void newMethod()
Class
implements
Compilation Error must
implement newMethod
Solution to not modify sub class
Interface
default void
newMethod() {
Code here….
}
Does not break any sub
class implementing this
Interface
 Default Method could be overridden in interface implementation
Lambda Expressions
Interface Static Method
Similar to default methods except that it is not possible to override them in
the implementation classes
providing security.
Interface static methods are good for providing utility methods.
Interface
void newMethod()
Class
implements
Compilation Error must
implement newMethod
Solution to not modify sub class
Interface
Static void
newMethod() {
Code here….
}
Does not break any sub
class implementing this
Interface
JAVA 8 STREAM API
JAVA 8 Stream API
What’s Stream API
Coming Soon 

More Related Content

What's hot (20)

PDF
Functional JavaScript Fundamentals
Srdjan Strbanovic
 
PPTX
Lambda Expressions in Java 8
icarter09
 
PPTX
Java 8 Features
Trung Nguyen
 
PDF
Modern Programming in Java 8 - Lambdas, Streams and Date Time API
Ganesh Samarthyam
 
ODP
Play framework training by Neelkanth Sachdeva @ Scala traits event , New Delh...
Knoldus Inc.
 
PDF
JDK8 Functional API
Justin Lin
 
PPTX
New Features of JAVA SE8
Dinesh Pathak
 
PDF
Intro to Java 8 Closures (Dainius Mezanskas)
Kaunas Java User Group
 
PPTX
Python Lambda Function
Md Soyaib
 
PPTX
Java concurrency questions and answers
CodeOps Technologies LLP
 
PPTX
Functional java 8
nick_maiorano
 
PDF
20160520 what youneedtoknowaboutlambdas
shinolajla
 
PDF
Smart Migration to JDK 8
Geertjan Wielenga
 
PPTX
Eclipse Day India 2015 - Java 8 Overview
Eclipse Day India
 
PDF
Functional programming in scala
Stratio
 
ODP
Refactoring to Scala DSLs and LiftOff 2009 Recap
Dave Orme
 
ODP
Drilling the Async Library
Knoldus Inc.
 
PDF
A Brief, but Dense, Intro to Scala
Derek Chen-Becker
 
PPTX
Introduction to Scala language
Aaqib Pervaiz
 
PPT
Pxb For Yapc2008
maximgrp
 
Functional JavaScript Fundamentals
Srdjan Strbanovic
 
Lambda Expressions in Java 8
icarter09
 
Java 8 Features
Trung Nguyen
 
Modern Programming in Java 8 - Lambdas, Streams and Date Time API
Ganesh Samarthyam
 
Play framework training by Neelkanth Sachdeva @ Scala traits event , New Delh...
Knoldus Inc.
 
JDK8 Functional API
Justin Lin
 
New Features of JAVA SE8
Dinesh Pathak
 
Intro to Java 8 Closures (Dainius Mezanskas)
Kaunas Java User Group
 
Python Lambda Function
Md Soyaib
 
Java concurrency questions and answers
CodeOps Technologies LLP
 
Functional java 8
nick_maiorano
 
20160520 what youneedtoknowaboutlambdas
shinolajla
 
Smart Migration to JDK 8
Geertjan Wielenga
 
Eclipse Day India 2015 - Java 8 Overview
Eclipse Day India
 
Functional programming in scala
Stratio
 
Refactoring to Scala DSLs and LiftOff 2009 Recap
Dave Orme
 
Drilling the Async Library
Knoldus Inc.
 
A Brief, but Dense, Intro to Scala
Derek Chen-Becker
 
Introduction to Scala language
Aaqib Pervaiz
 
Pxb For Yapc2008
maximgrp
 

Similar to Java 8 Bootcamp (20)

PDF
Java 8 Lambda Expressions & Streams
NewCircle Training
 
PPT
Major Java 8 features
Sanjoy Kumar Roy
 
PPTX
Lambda Expressions Java 8 Features usage
AsmaShaikh478737
 
PDF
Functional programming in java 8 by harmeet singh
Harmeet Singh(Taara)
 
PDF
Jf12 lambdas injava8-1
langer4711
 
PPTX
Java 8
AbhimanuHandoo
 
PDF
Java 8-revealed
Hamed Hatami
 
PDF
Lambdas in Java 8
Tobias Coetzee
 
PDF
Java 8 features
NexThoughts Technologies
 
PDF
Programming with Lambda Expressions in Java
langer4711
 
PPTX
Java 8 presentation
Van Huong
 
PDF
Java 8
Sheeban Singaram
 
PDF
Lambda Expressions in Java
Erhan Bagdemir
 
PPTX
Java 8 features
Maýur Chourasiya
 
PPTX
Java 8 lambda
Manav Prasad
 
PPTX
Java 8 new features
Aniket Thakur
 
PPTX
Project Lambda: Evolution of Java
Can Pekdemir
 
PDF
Java8
Felipe Mamud
 
PPTX
Lambda expressions java8 - yousry
yousry ibrahim
 
PPTX
Project Lambda: Functional Programming Constructs in Java - Simon Ritter (Ora...
jaxLondonConference
 
Java 8 Lambda Expressions & Streams
NewCircle Training
 
Major Java 8 features
Sanjoy Kumar Roy
 
Lambda Expressions Java 8 Features usage
AsmaShaikh478737
 
Functional programming in java 8 by harmeet singh
Harmeet Singh(Taara)
 
Jf12 lambdas injava8-1
langer4711
 
Java 8-revealed
Hamed Hatami
 
Lambdas in Java 8
Tobias Coetzee
 
Java 8 features
NexThoughts Technologies
 
Programming with Lambda Expressions in Java
langer4711
 
Java 8 presentation
Van Huong
 
Lambda Expressions in Java
Erhan Bagdemir
 
Java 8 features
Maýur Chourasiya
 
Java 8 lambda
Manav Prasad
 
Java 8 new features
Aniket Thakur
 
Project Lambda: Evolution of Java
Can Pekdemir
 
Lambda expressions java8 - yousry
yousry ibrahim
 
Project Lambda: Functional Programming Constructs in Java - Simon Ritter (Ora...
jaxLondonConference
 
Ad

More from Mohamed Ben Hassine (6)

PDF
Agile_Team
Mohamed Ben Hassine
 
PDF
scrum_certificate
Mohamed Ben Hassine
 
PDF
Udemy_BigData
Mohamed Ben Hassine
 
PDF
Mohamed -CV 2016
Mohamed Ben Hassine
 
PDF
My CV 2016
Mohamed Ben Hassine
 
scrum_certificate
Mohamed Ben Hassine
 
Udemy_BigData
Mohamed Ben Hassine
 
Mohamed -CV 2016
Mohamed Ben Hassine
 
Ad

Java 8 Bootcamp

  • 1. Mohamed Ben Hassine Java / JEE Technical Lead Trainer [email protected] (+216) 52 633 220 www.linkedin.com/in/mohamedbnhassine Java 8 Bootcamp
  • 2. Java 8 Bootcamp What you will Learn • Get Started • Lambda Expressions • Java 8 Stream API • New Date and Time API • Nashorn: New JavaScript Engine • Miscellaneous
  • 4. History of Java releases 4 1995 JDKIntroduction January1996 JDK1.0 “OAK”JDK1.0 December1998 J2SE1.2 FoundationofJCJ Swing,Collections February2002 J2SE1.4 Assert,JAXP,IPv6 RegularExpressions February 1997 JDK 1.1 Java Beans, RMI , JDBC, JIT May 2000 J2SE 1.3 HotSpot JVM , JNDI, Java Debugger September 2004 J2SE 5.0 Generics, Metadata, Concurrency, Static Import September 2006 Java SE 6 Java Compiler API ,Support JDBC 4.0 , JVM improvements July 2011 Java SE 7 Try-with-Resources Strings in switch New IO API , Dynamic languages March 2015 Java SE 8 Project Lambda Streams API , Functional Interfaces ,Nashorn Engine ,New Date Time API , Annotations on Type 2017 Java SE 9 Jshell Java REPL , Support HTTP 2.0 , G1 default Garbage Collector Get Started
  • 5. Why Java 8 Now ? 5 Get Started Revolution in Processing power Parallel multicores architectures Big Data Applications Code behavior Code as data Functional programming Support Enhance core librairies Interface unlocking (concrete methods ) Backward compatibility Java / Javascript JavaScript made great in Java 8
  • 6. Install and configure Java 8 6 Get Started
  • 7. Project Based Course 7 Get Started Employee management Throughout the whole course , we will develop a java console app which offers suck key features :  Add manage employee  Manage employees personal details , salaries and bonus  Search employee by predefined criteria
  • 8. Lambda Expressions First Lambda Expression Comparator<Employee> compLambda = ( emp1, emp2) -> { int i = Integer.compare(emp1.getAge(), emp2.getAge()); if (i != 0) { return i; } else { i = Double.compare(emp1.getSalary(), emp2.getSalary()); if (i != 0) return i; } return i; }; Collections.sort(persons, compLambda); Much Easier ! persons.sort(Comparator.comparing(Employee::getAge).thenComparing(Employee::getSalary)); Don’t worry about this code for now. This Course will explain what it does and how you can develop similar code!
  • 9. Lambda Expressions Why Lambda Expression  Using anonymous classes for code behavior parameterization is verbose  Programmers struggle with anonymous classes. (confusion “this”, no access to non-final local variables)  Convenient for new streams library persons.forEach(emp -> emp.setSalary(3000)); Solution : Lambda let you represent a behavior or pass code in a concise way. Another way to write anonymous classes Comparator<Employee> compLambda = ( emp1, emp2) -> { return emp1.getJobTitle().compareTo(emp2.getJobTitle()); } Lambda Parameters Arrow Lambda Body
  • 10. Lambda Expressions How to Write Lambda Expression The simple way More than One line of code Runnable r = () -> { for (int i = 0; i < 2016; i++) { System.out.println("Hello to your Java SE 8 Bootcamp 2016 !"); } }; More than One Argument Comparator<Employee> compLambda = (Employee emp1, Employee emp2) -> { return emp1.getJobTitle().compareTo(emp2.getJobTitle()); } ; FileFilter filter = (File file) -> file.getName().endsWith("class");
  • 11. Lambda Expressions So what is exactly Lambda Expression ? Everything in Java has a type so what for Lambda ? : Lambda Expression is Functional Interface ( new Java 8 concept , Interface with one abstract Method..example Runnable , Comparator interfaces known prior to Java SE8 as Single Abstract Method Interface). Where to use A Lambda Expression ? In each case it could be assigned to a variable of Type Functional Interface Runnable r = () -> System.out.println("Hello to your Java SE 8 Bootcamp 2016 !"); ; Lambda Expression
  • 12. Lambda Expressions Functional Interface An Interface with only one abstract method Example : public interface Runnable { public abstract void run(); } @FunctionalInterface public interface SimpleInterface { public void doSomeJob() ; } Define Java SE8 Functional Interface
  • 13. Lambda Expressions Predefined Functional Interface in JAVA SE8 New Java Package : java.util.function Divided into 4 categories : Supplier Consumer Predicate /** @since 1.8 */ @FunctionalInterface public interface Supplier<T> { /** * Gets a result. * * @return a result */ T get(); } Function /** @since 1.8 */ @FunctionalInterface public interface Consumer<T> { /** * @param t the input argument */ void accept(T t); } /** @since 1.8 */ @FunctionalInterface public interface Predicate<T> { /** * Evaluates this predicate on the given argument. */ boolean test(T t); } /** @since 1.8 */ @FunctionalInterface public interface Function<T, R> { /** * Applies this function to the given argument. */ R apply(T t); }
  • 14. Lambda Expressions Method Reference Simply : An other way to write Lambda Expression Comparator<Employee> compLambda = (Employee emp1, Employee emp2) -> { return Double.compare(emp1.getSalary(), emp2.getSalary()) ; } ; Comparator<Employee> compLambdaRef = Comparator.comparing(Employee::getSalary); – Replace (args) -> ClassName.staticMethodName(args) – with ClassName::staticMethodName
  • 15. Lambda Expressions So how to process Data with this Lambda ? List<Employee> persons = new ArrayList<>(); persons.add(new Employee("Mohamed", 30, 2500, new Date(), true, "Java / JEE TL")); persons.add(new Employee("Ferdaous", 24, 1900, new Date(), false, "IT Consultant")); // Prior to Java SE 8 for (Employee employee : persons) { System.out.println(employee) ;} Using Lambda Expression // Traverse collection with Lambda Expression persons.forEach(emp -> System.out.println(emp)); // More concise syntax with Method Reference persons.forEach(System.out::println); Where does this method come from ? All interface implementations of Iterable should be refactored !.. Response Next episode ..
  • 16. Lambda Expressions Default Method In Java 8 a method can be implemented in an interface.  New Java 8 Concept for backward compatibility Allow to add methods to an existing interface long after it has been released without breaking the interface Interface void newMethod() Class implements Compilation Error must implement newMethod Solution to not modify sub class Interface default void newMethod() { Code here…. } Does not break any sub class implementing this Interface  Default Method could be overridden in interface implementation
  • 17. Lambda Expressions Interface Static Method Similar to default methods except that it is not possible to override them in the implementation classes providing security. Interface static methods are good for providing utility methods. Interface void newMethod() Class implements Compilation Error must implement newMethod Solution to not modify sub class Interface Static void newMethod() { Code here…. } Does not break any sub class implementing this Interface
  • 19. JAVA 8 Stream API What’s Stream API Coming Soon 