SlideShare uma empresa Scribd logo
1 de 23
Java 8 - A Brief Overview
Allahbaksh Asadullah and Avitash Purohit
2
Contents
• Introduction
• Functional Interface
• Lambda Expressions
• Stream API
• Optional class
• Q & A
Story of λ and Win of King
Default Methods
5
Default methods in an Interface
• Backward compatibility to interface APIs
• Less number of classes /interfaces
• Prevents reimplementation of complete APIs
• API developer can provide reference implementation of methods
interface BankingAPI{
BigDecimal calculateMaturityAmount(BigDecimal inAmount);
default BigDecimal calculateTDSOnAmount(BigDecimal inAmount){
return inAmount.multiply(new BigDecimal(0.1));
}
}
Functional Interfaces
7
Functional Interfaces
Functional interfaces provide target types for lambda expressions and method
references.
1. It has a single abstract method called the functional method
2. The lambda expression parameter and return type are matched for the
functional interface.
3. Can have any number of default methods.
4. Functional interfaces are annotated with @FunctionalInterface
@FunctionalInterface
public interface Iterable<T> {
Iterator<T> iterator();
}
8
Lambda Expressions
• A Lambda expression is written as
• Where argument type can be inferred by the compiler
• Has access to the class variables using this
• Target type of Lambda is FunctionalInterface
TypeName objName = (argument..)->{Body};
9
Examples of Lambda Expressions
MathOperation o = (a,b) ->{return a*b;};
o.operation(4,5);
BiFunction<Integer, Integer, Integer> o1 = (a,b) ->{return a+b;};
BiFunction<Integer, Integer, Integer> o1 =(a,b) -> a+b;
int sum = o1.apply(4, 5);
MathOperation o = (a,b) ->{return a*b+effectiveFinalInt+1; };
10
Lets Lambdify …
Runnable runnable = new Runnable() {
public void run() {
System.out.println("Hello you are in
Runnable");
}
};
Runnable runnable = () ->{
System.out.println("I am in Lambda
land");
};
This creates a
<ClassName>$<Number>.class file for
the above anonymous class
Lets see how Lambda works internally
11
Decompiling the λ expression
• Run the class with following structure
• The Java runtime creates a .class file whose decompiled code looks like
• The class file structure of LambdaTest.class gives the following information
java --Djdk.internal.lambda.dumpProxyClasses=<FolderPath> <ClassNames or JarName>
final class LambdaTest$$Lambda$1 implements Runnable {
private LambdaTest$$Lambda$1() {
}
public void run() {
LambdaTest.lambda$0();
}
}
??
12
Lambda some more information
● invokedynamic (Java 7) is used to for Lambda expression implementation
● Implementation details can be JVM specific and are deferred until runtime
● Compiler encounters lambda expression, lambda is 'desugared' into a private
static method with appropriate signature, Compiler indicates bootstrap method
and provides static arguments about lambda
● Compiler generates invokedynamic instruction referencing bootstrap method
Stream API
14
Stream API
• Provides a wrapper around a Collection that carries values from a source.
• It provides a fluent interface to create a pipeline or chain of the operations.
• Powerful, faster and more memory efficient and are designed for lambdas
• Employs lazy evaluation and are implicitly parallelizable to multiple cores.
• Inbuilt SQL like aggregation functions like sum, sort, max, min, average
15
List<Employee> list = employeeList.stream().parallelStream().map(e -> {e.setSalary(.map(R F.apply(T t))
Examples of Stream API
e.getSalary() * 1.2); return e;}).
Can add any operation on stream like map,
reduce or can collect the employees
collect(Collectors.toList());
List<String> teamList = new ArrayList<>();
/* Add List Members
*
*/
Stream<String> teamStream = teamList.stream() ;
Stream<String> teamStream = Stream.of("Allahbaksh","Avitash","Vivek");
Set<String> teamSet = teamStream.collect(Collectors.toSet());
Some more examples
Optional Class
17
Wise Man Says..
One of my billion $ mistake was to creation
of Null reference, I created it because it
was so tempting and easy to implement
18
Tired of checking NullPointerException?
• String location = employee.getManager().getUnit().getLocation();
• What if Manager is not present and getManager() returns null.
• Lack of Documentation or lack of Reading a documentation.
• To remove such errors we use nested checks like
if(employee.getManager()!=null){
if(employee.getManager().getUnit()!=null){
if(employee.getManager().getUnit().getLocation()!=null){
// Business Logic
}
}
}
19
Example of Optional
public class Unit{
public Optional<String> location;
public Optional<String> getLocation(){
return location;
}
}
public class Employee{
public Optional<Manager> manager;
public Optional<Manager> getMananger(){
return manager;
}
}
String location =
employee.flatMap(Employee::getManager).flatMap(Manager::getUnit)
.map(Unit::getLocation);
public class Manager extends Employee{
public Optional<Unit> unit;
public Optional<Unit> getUnit(){
return unit;
}
}
20
Some methods of Optional class
• boolean isPresent()
• T get()
• void ifPresent(Consumer<? super T> consumer)
• Optional.of(T value)
This is not the END of king
Q & A
© 2014 Infosys Limited, Bangalore, India. All Rights Reserved. Infosys believes the information in this document is accurate as of its publication date; such information is subject to change
without notice. Infosys acknowledges the proprietary rights of other companies to the trademarks, product names and such other intellectual property rights mentioned in this document. Except
as expressly permitted, neither this documentation nor any part of it may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, printing,
photocopying,recording or otherwise, withoutthe prior permission of Infosys Limited and/ or any named intellectual property rights holders under this document.
Thanks

Mais conteúdo relacionado

Mais procurados

Language Integrated Query - LINQ
Language Integrated Query - LINQLanguage Integrated Query - LINQ
Language Integrated Query - LINQ
Doncho Minkov
 
Codecamp iasi-26 nov 2011-what's new in jpa 2.0
Codecamp iasi-26 nov 2011-what's new in jpa 2.0Codecamp iasi-26 nov 2011-what's new in jpa 2.0
Codecamp iasi-26 nov 2011-what's new in jpa 2.0
Codecamp Romania
 
Repository design pattern in laravel
Repository design pattern in laravelRepository design pattern in laravel
Repository design pattern in laravel
Sameer Poudel
 

Mais procurados (20)

Java 8 new features
Java 8 new featuresJava 8 new features
Java 8 new features
 
Link quries
Link quriesLink quries
Link quries
 
LINQ
LINQLINQ
LINQ
 
Java 8 New features
Java 8 New featuresJava 8 New features
Java 8 New features
 
WHY JAVASCRIPT FUNCTIONAL PROGRAMMING IS SO HARD?
WHY JAVASCRIPT FUNCTIONAL PROGRAMMING IS SO HARD? WHY JAVASCRIPT FUNCTIONAL PROGRAMMING IS SO HARD?
WHY JAVASCRIPT FUNCTIONAL PROGRAMMING IS SO HARD?
 
Language Integrated Query - LINQ
Language Integrated Query - LINQLanguage Integrated Query - LINQ
Language Integrated Query - LINQ
 
Introduction to functional programming with java 8
Introduction to functional programming with java 8Introduction to functional programming with java 8
Introduction to functional programming with java 8
 
Grokking Techtalk #38: Escape Analysis in Go compiler
 Grokking Techtalk #38: Escape Analysis in Go compiler Grokking Techtalk #38: Escape Analysis in Go compiler
Grokking Techtalk #38: Escape Analysis in Go compiler
 
Java 8 concurrency abstractions
Java 8 concurrency abstractionsJava 8 concurrency abstractions
Java 8 concurrency abstractions
 
Codecamp iasi-26 nov 2011-what's new in jpa 2.0
Codecamp iasi-26 nov 2011-what's new in jpa 2.0Codecamp iasi-26 nov 2011-what's new in jpa 2.0
Codecamp iasi-26 nov 2011-what's new in jpa 2.0
 
Spring Day | Spring and Scala | Eberhard Wolff
Spring Day | Spring and Scala | Eberhard WolffSpring Day | Spring and Scala | Eberhard Wolff
Spring Day | Spring and Scala | Eberhard Wolff
 
Writing code that writes code - Nguyen Luong
Writing code that writes code - Nguyen LuongWriting code that writes code - Nguyen Luong
Writing code that writes code - Nguyen Luong
 
Zen and the Art of REST API documentation - MuCon London 2015
Zen and the Art of REST API documentation - MuCon London 2015Zen and the Art of REST API documentation - MuCon London 2015
Zen and the Art of REST API documentation - MuCon London 2015
 
Introduction to Java 11
Introduction to Java 11 Introduction to Java 11
Introduction to Java 11
 
Module 3: Introduction to LINQ (PowerPoint Slides)
Module 3: Introduction to LINQ (PowerPoint Slides)Module 3: Introduction to LINQ (PowerPoint Slides)
Module 3: Introduction to LINQ (PowerPoint Slides)
 
Repository design pattern in laravel
Repository design pattern in laravelRepository design pattern in laravel
Repository design pattern in laravel
 
Java 8 Bootcamp
Java 8 BootcampJava 8 Bootcamp
Java 8 Bootcamp
 
Books
BooksBooks
Books
 
Introducing LINQ
Introducing LINQIntroducing LINQ
Introducing LINQ
 
Functional Programming in Java
Functional Programming in JavaFunctional Programming in Java
Functional Programming in Java
 

Destaque

CompletableFuture
CompletableFutureCompletableFuture
CompletableFuture
koji lin
 
Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streams
Shahjahan Samoon
 

Destaque (20)

Java 8: Lambdas, Monads and Java Collections - 12.05.2015 @JInkubator
Java 8: Lambdas, Monads and Java Collections - 12.05.2015 @JInkubatorJava 8: Lambdas, Monads and Java Collections - 12.05.2015 @JInkubator
Java 8: Lambdas, Monads and Java Collections - 12.05.2015 @JInkubator
 
Java CMS 2015
Java CMS 2015Java CMS 2015
Java CMS 2015
 
Java8 javatime-api
Java8 javatime-apiJava8 javatime-api
Java8 javatime-api
 
Java8
Java8Java8
Java8
 
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
 
Xtend - better java with -less- noise
Xtend - better java with -less- noiseXtend - better java with -less- noise
Xtend - better java with -less- noise
 
Streams in Java 8
Streams in Java 8Streams in Java 8
Streams in Java 8
 
Java 8 Date and Time API
Java 8 Date and Time APIJava 8 Date and Time API
Java 8 Date and Time API
 
2015 Java update and roadmap, JUG sevilla
2015  Java update and roadmap, JUG sevilla2015  Java update and roadmap, JUG sevilla
2015 Java update and roadmap, JUG sevilla
 
Functional Java 8 in everyday life
Functional Java 8 in everyday lifeFunctional Java 8 in everyday life
Functional Java 8 in everyday life
 
Major Java 8 features
Major Java 8 featuresMajor Java 8 features
Major Java 8 features
 
CompletableFuture
CompletableFutureCompletableFuture
CompletableFuture
 
Java8
Java8Java8
Java8
 
Lean UX and Design winning mobile apps
Lean UX and Design winning mobile appsLean UX and Design winning mobile apps
Lean UX and Design winning mobile apps
 
10 Sets of Best Practices for Java 8
10 Sets of Best Practices for Java 810 Sets of Best Practices for Java 8
10 Sets of Best Practices for Java 8
 
Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streams
 
What's New in Java 8
What's New in Java 8What's New in Java 8
What's New in Java 8
 
Java 8 ​and ​Best Practices
 Java 8 ​and ​Best Practices Java 8 ​and ​Best Practices
Java 8 ​and ​Best Practices
 
ARM Processor
ARM ProcessorARM Processor
ARM Processor
 
Java SE 8 best practices
Java SE 8 best practicesJava SE 8 best practices
Java SE 8 best practices
 

Semelhante a Eclipse Day India 2015 - Java 8 Overview

Lecture from javaday.bg by Nayden Gochev/ Ivan Ivanov and Mitia Alexandrov
Lecture from javaday.bg by Nayden Gochev/ Ivan Ivanov and Mitia Alexandrov Lecture from javaday.bg by Nayden Gochev/ Ivan Ivanov and Mitia Alexandrov
Lecture from javaday.bg by Nayden Gochev/ Ivan Ivanov and Mitia Alexandrov
Nayden Gochev
 
Software Uni Conf October 2014
Software Uni Conf October 2014Software Uni Conf October 2014
Software Uni Conf October 2014
Nayden Gochev
 

Semelhante a Eclipse Day India 2015 - Java 8 Overview (20)

New features in jdk8 iti
New features in jdk8 itiNew features in jdk8 iti
New features in jdk8 iti
 
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
 
Developing android apps with java 8
Developing android apps with java 8Developing android apps with java 8
Developing android apps with java 8
 
What is new in java 8 concurrency
What is new in java 8 concurrencyWhat is new in java 8 concurrency
What is new in java 8 concurrency
 
New Features in JDK 8
New Features in JDK 8New Features in JDK 8
New Features in JDK 8
 
Lambdas and Laughs
Lambdas and LaughsLambdas and Laughs
Lambdas and Laughs
 
Lambdas in Java 8
Lambdas in Java 8Lambdas in Java 8
Lambdas in Java 8
 
Java 8 Intro - Core Features
Java 8 Intro - Core FeaturesJava 8 Intro - Core Features
Java 8 Intro - Core Features
 
14274730 (1).ppt
14274730 (1).ppt14274730 (1).ppt
14274730 (1).ppt
 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentation
 
Java 8 - An Overview
Java 8 - An OverviewJava 8 - An Overview
Java 8 - An Overview
 
Lambda Expressions Java 8 Features usage
Lambda Expressions  Java 8 Features usageLambda Expressions  Java 8 Features usage
Lambda Expressions Java 8 Features usage
 
Functional Programming With Lambdas and Streams in JDK8
 Functional Programming With Lambdas and Streams in JDK8 Functional Programming With Lambdas and Streams in JDK8
Functional Programming With Lambdas and Streams in JDK8
 
Java 8
Java 8Java 8
Java 8
 
Java 8 Overview
Java 8 OverviewJava 8 Overview
Java 8 Overview
 
Scala and Spring
Scala and SpringScala and Spring
Scala and Spring
 
Lecture from javaday.bg by Nayden Gochev/ Ivan Ivanov and Mitia Alexandrov
Lecture from javaday.bg by Nayden Gochev/ Ivan Ivanov and Mitia Alexandrov Lecture from javaday.bg by Nayden Gochev/ Ivan Ivanov and Mitia Alexandrov
Lecture from javaday.bg by Nayden Gochev/ Ivan Ivanov and Mitia Alexandrov
 
Java 8
Java 8Java 8
Java 8
 
Software Uni Conf October 2014
Software Uni Conf October 2014Software Uni Conf October 2014
Software Uni Conf October 2014
 
Colloquium Report
Colloquium ReportColloquium Report
Colloquium Report
 

Mais de Eclipse Day India

Please Behave Yourself: BDD and automating Eclipse RCP applications using JBe...
Please Behave Yourself: BDD and automating Eclipse RCP applications using JBe...Please Behave Yourself: BDD and automating Eclipse RCP applications using JBe...
Please Behave Yourself: BDD and automating Eclipse RCP applications using JBe...
Eclipse Day India
 
Supporting Java™ 9 in Eclipse - A critical perspective - Stephan Herrmann
Supporting Java™ 9 in Eclipse - A critical perspective - Stephan HerrmannSupporting Java™ 9 in Eclipse - A critical perspective - Stephan Herrmann
Supporting Java™ 9 in Eclipse - A critical perspective - Stephan Herrmann
Eclipse Day India
 

Mais de Eclipse Day India (20)

Java Performance Testing for Everyone - Shelley Lambert
Java Performance Testing for Everyone - Shelley LambertJava Performance Testing for Everyone - Shelley Lambert
Java Performance Testing for Everyone - Shelley Lambert
 
Eclipse IDE Tips and Tricks - Lakshmi Priya Shanmugam
Eclipse IDE Tips and Tricks - Lakshmi Priya ShanmugamEclipse IDE Tips and Tricks - Lakshmi Priya Shanmugam
Eclipse IDE Tips and Tricks - Lakshmi Priya Shanmugam
 
Pattern Matching in Java - Srikanth Sankaran
Pattern Matching in Java - Srikanth SankaranPattern Matching in Java - Srikanth Sankaran
Pattern Matching in Java - Srikanth Sankaran
 
Machine Learning for Java Developers - Nasser Ebrahim
Machine Learning for Java Developers - Nasser EbrahimMachine Learning for Java Developers - Nasser Ebrahim
Machine Learning for Java Developers - Nasser Ebrahim
 
Scaling Eclipse on HiDPI Monitors - Niraj Modi
Scaling Eclipse on HiDPI Monitors - Niraj ModiScaling Eclipse on HiDPI Monitors - Niraj Modi
Scaling Eclipse on HiDPI Monitors - Niraj Modi
 
Please Behave Yourself: BDD and automating Eclipse RCP applications using JBe...
Please Behave Yourself: BDD and automating Eclipse RCP applications using JBe...Please Behave Yourself: BDD and automating Eclipse RCP applications using JBe...
Please Behave Yourself: BDD and automating Eclipse RCP applications using JBe...
 
Supporting Java™ 9 in Eclipse - A critical perspective - Stephan Herrmann
Supporting Java™ 9 in Eclipse - A critical perspective - Stephan HerrmannSupporting Java™ 9 in Eclipse - A critical perspective - Stephan Herrmann
Supporting Java™ 9 in Eclipse - A critical perspective - Stephan Herrmann
 
Eclipse Day India 2015 - Rest with Java (jax rs) and jersey
Eclipse Day India 2015 - Rest with Java (jax rs) and jerseyEclipse Day India 2015 - Rest with Java (jax rs) and jersey
Eclipse Day India 2015 - Rest with Java (jax rs) and jersey
 
Eclipse Day India 2015 - Java bytecode analysis and JIT
Eclipse Day India 2015 - Java bytecode analysis and JITEclipse Day India 2015 - Java bytecode analysis and JIT
Eclipse Day India 2015 - Java bytecode analysis and JIT
 
Eclipse Day India 2015 - Java 9
Eclipse Day India 2015 - Java 9Eclipse Day India 2015 - Java 9
Eclipse Day India 2015 - Java 9
 
Eclipse Day India 2015 - Keynote - Stephan Herrmann
Eclipse Day India 2015 - Keynote - Stephan HerrmannEclipse Day India 2015 - Keynote - Stephan Herrmann
Eclipse Day India 2015 - Keynote - Stephan Herrmann
 
Eclipse Day India 2015 - Eclipse RCP testing using Jubula based automation
Eclipse Day India 2015 - Eclipse RCP testing using Jubula based automationEclipse Day India 2015 - Eclipse RCP testing using Jubula based automation
Eclipse Day India 2015 - Eclipse RCP testing using Jubula based automation
 
Eclipse Day India 2015 - Oomph
Eclipse Day India 2015 - OomphEclipse Day India 2015 - Oomph
Eclipse Day India 2015 - Oomph
 
Eclipse Day India 2015 - Keynote (Mike Milinkovich)
Eclipse Day India 2015 - Keynote (Mike Milinkovich)Eclipse Day India 2015 - Keynote (Mike Milinkovich)
Eclipse Day India 2015 - Keynote (Mike Milinkovich)
 
Eclipse Day India 2015 - Unleashing the Java 8 Tooling in Eclipse
Eclipse Day India 2015 - Unleashing the Java 8 Tooling in EclipseEclipse Day India 2015 - Unleashing the Java 8 Tooling in Eclipse
Eclipse Day India 2015 - Unleashing the Java 8 Tooling in Eclipse
 
IDS and Bluemix
IDS and BluemixIDS and Bluemix
IDS and Bluemix
 
SWT - Technical Deep Dive
SWT - Technical Deep DiveSWT - Technical Deep Dive
SWT - Technical Deep Dive
 
PDE builds or Maven
PDE builds or MavenPDE builds or Maven
PDE builds or Maven
 
Orion - IDE on the cloud
Orion - IDE on the cloudOrion - IDE on the cloud
Orion - IDE on the cloud
 
KlighD
KlighDKlighD
KlighD
 

Último

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Último (20)

AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
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
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
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
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 

Eclipse Day India 2015 - Java 8 Overview

  • 1. Java 8 - A Brief Overview Allahbaksh Asadullah and Avitash Purohit
  • 2. 2 Contents • Introduction • Functional Interface • Lambda Expressions • Stream API • Optional class • Q & A
  • 3. Story of λ and Win of King
  • 5. 5 Default methods in an Interface • Backward compatibility to interface APIs • Less number of classes /interfaces • Prevents reimplementation of complete APIs • API developer can provide reference implementation of methods interface BankingAPI{ BigDecimal calculateMaturityAmount(BigDecimal inAmount); default BigDecimal calculateTDSOnAmount(BigDecimal inAmount){ return inAmount.multiply(new BigDecimal(0.1)); } }
  • 7. 7 Functional Interfaces Functional interfaces provide target types for lambda expressions and method references. 1. It has a single abstract method called the functional method 2. The lambda expression parameter and return type are matched for the functional interface. 3. Can have any number of default methods. 4. Functional interfaces are annotated with @FunctionalInterface @FunctionalInterface public interface Iterable<T> { Iterator<T> iterator(); }
  • 8. 8 Lambda Expressions • A Lambda expression is written as • Where argument type can be inferred by the compiler • Has access to the class variables using this • Target type of Lambda is FunctionalInterface TypeName objName = (argument..)->{Body};
  • 9. 9 Examples of Lambda Expressions MathOperation o = (a,b) ->{return a*b;}; o.operation(4,5); BiFunction<Integer, Integer, Integer> o1 = (a,b) ->{return a+b;}; BiFunction<Integer, Integer, Integer> o1 =(a,b) -> a+b; int sum = o1.apply(4, 5); MathOperation o = (a,b) ->{return a*b+effectiveFinalInt+1; };
  • 10. 10 Lets Lambdify … Runnable runnable = new Runnable() { public void run() { System.out.println("Hello you are in Runnable"); } }; Runnable runnable = () ->{ System.out.println("I am in Lambda land"); }; This creates a <ClassName>$<Number>.class file for the above anonymous class Lets see how Lambda works internally
  • 11. 11 Decompiling the λ expression • Run the class with following structure • The Java runtime creates a .class file whose decompiled code looks like • The class file structure of LambdaTest.class gives the following information java --Djdk.internal.lambda.dumpProxyClasses=<FolderPath> <ClassNames or JarName> final class LambdaTest$$Lambda$1 implements Runnable { private LambdaTest$$Lambda$1() { } public void run() { LambdaTest.lambda$0(); } } ??
  • 12. 12 Lambda some more information ● invokedynamic (Java 7) is used to for Lambda expression implementation ● Implementation details can be JVM specific and are deferred until runtime ● Compiler encounters lambda expression, lambda is 'desugared' into a private static method with appropriate signature, Compiler indicates bootstrap method and provides static arguments about lambda ● Compiler generates invokedynamic instruction referencing bootstrap method
  • 14. 14 Stream API • Provides a wrapper around a Collection that carries values from a source. • It provides a fluent interface to create a pipeline or chain of the operations. • Powerful, faster and more memory efficient and are designed for lambdas • Employs lazy evaluation and are implicitly parallelizable to multiple cores. • Inbuilt SQL like aggregation functions like sum, sort, max, min, average
  • 15. 15 List<Employee> list = employeeList.stream().parallelStream().map(e -> {e.setSalary(.map(R F.apply(T t)) Examples of Stream API e.getSalary() * 1.2); return e;}). Can add any operation on stream like map, reduce or can collect the employees collect(Collectors.toList()); List<String> teamList = new ArrayList<>(); /* Add List Members * */ Stream<String> teamStream = teamList.stream() ; Stream<String> teamStream = Stream.of("Allahbaksh","Avitash","Vivek"); Set<String> teamSet = teamStream.collect(Collectors.toSet()); Some more examples
  • 17. 17 Wise Man Says.. One of my billion $ mistake was to creation of Null reference, I created it because it was so tempting and easy to implement
  • 18. 18 Tired of checking NullPointerException? • String location = employee.getManager().getUnit().getLocation(); • What if Manager is not present and getManager() returns null. • Lack of Documentation or lack of Reading a documentation. • To remove such errors we use nested checks like if(employee.getManager()!=null){ if(employee.getManager().getUnit()!=null){ if(employee.getManager().getUnit().getLocation()!=null){ // Business Logic } } }
  • 19. 19 Example of Optional public class Unit{ public Optional<String> location; public Optional<String> getLocation(){ return location; } } public class Employee{ public Optional<Manager> manager; public Optional<Manager> getMananger(){ return manager; } } String location = employee.flatMap(Employee::getManager).flatMap(Manager::getUnit) .map(Unit::getLocation); public class Manager extends Employee{ public Optional<Unit> unit; public Optional<Unit> getUnit(){ return unit; } }
  • 20. 20 Some methods of Optional class • boolean isPresent() • T get() • void ifPresent(Consumer<? super T> consumer) • Optional.of(T value)
  • 21. This is not the END of king
  • 22. Q & A
  • 23. © 2014 Infosys Limited, Bangalore, India. All Rights Reserved. Infosys believes the information in this document is accurate as of its publication date; such information is subject to change without notice. Infosys acknowledges the proprietary rights of other companies to the trademarks, product names and such other intellectual property rights mentioned in this document. Except as expressly permitted, neither this documentation nor any part of it may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, printing, photocopying,recording or otherwise, withoutthe prior permission of Infosys Limited and/ or any named intellectual property rights holders under this document. Thanks