KEMBAR78
Java8 Lambda and Streams | ODP
Introduction of Java8
Lambda Expression
Presenter: Prem Chand Mali, Mindfire Solutions
Date: 19/06/2014
Presenter: Prem Chand Mali, Mindfire Solutions
About Me
SCJP/OCJP - Oracle Certified Java Programmer
MCP:70-480 - Specialist certification in HTML5
with JavaScript and CSS3 Exam
Skills : Java, Swings, Springs,
Hibernate, JavaFX, Jquery,
prototypeJS, ExtJS.
Connect Me :
https://www.facebook.com/prem.c.mali
http://www.linkedin.com/in/premmali
https://twitter.com/prem_mali
https://plus.google.com/106150245941317924019/about/p/pub
Contact Me :
premchandm@mindfiresolutions.com / prem.c.mali@gmail.com
mfsi_premchandm
Agenda
Presenter: Prem Chand Mali, Mindfire Solutions
History
What is Lambada ?
Syntax
Function Interface
Default Methods
Variable capture
Method References
Q & A
History
• Lambda calculus (also written as λ-calculus) is a formal system in mathematical
logic and computer science for expressing computation based on function
abstraction and application using variable binding and substitution.
• The lambda calculus was introduced by mathematician Alonzo Church in the 1930s
as part of an investigation into the foundations of mathematics.
• Programming languages ( C#, Javascript, Ruby, C++, etc) have support for lambda
expressions.
• Java 8 have lambda expressions.
Presenter: Prem Chand Mali, Mindfire Solutions
What is Lamdba expression ?
• Lambda expressions are anonymous functions.
• Lambda expressions can be assigned to variable, passed to function and it can be
returned from function.
Presenter: Prem Chand Mali, Mindfire Solutions
Syntax
() -> { System.out.printlns("Hello World!");}
(int a, int b) -> a + b
() -> { return 1; }
(String name) -> { System.out.println("Hello "+name); }
n -> n % 2 != 0
Presenter: Prem Chand Mali, Mindfire Solutions
JDK single method interfaces
public interface Runnable { void run(); }
public interface Callable<V> { V call() throws Exception; }
public interface ActionListener { void actionPerformed(ActionEvent e); }
public interface Comparator<T> { int compare(T o1, T o2); boolean equals(Object
obj); }
Presenter: Prem Chand Mali, Mindfire Solutions
Functional Interface
• Functional Interfaces Annotate by @FunctionInterface
• Functional Interfaces which has only one method.
• Functional Interfaces can have one or more default methods.
Presenter: Prem Chand Mali, Mindfire Solutions
Functional Interface
@FunctionalInterface
Public interface MyDemo {
public void doSomething();
//public void doSomethingForDemo(); Compiler error.
}
Presenter: Prem Chand Mali, Mindfire Solutions
Default Methods
• Default method enable you to add new functionality to the interfaces of your
libraries and ensure binary compatibility with code written for older versions of
those interfaces.
• Default key word needs to use to make default method.
• Default method shouldn't be abstract.
Presenter: Prem Chand Mali, Mindfire Solutions
Default Methods
@FunctionalInterface
Public interface MyDemo {
public void doSomething();
public default String myDefault() {
System.out.println(“Hello world from default”);
}
}
Presenter: Prem Chand Mali, Mindfire Solutions
Variable Capture
• In Java8 Local variable are accessible from Lambda expressions. In Java7 compiler
complains.
• When you try to modify local variable value compiler will complain if it not final.
Presenter: Prem Chand Mali, Mindfire Solutions
Variable Capture
Public Class MyDemo {
public static void main(String[] args) {
String myName = “Runnable”;
Runnable r = () → System.out.println(“ Name : ” + myName);
//myName = “something”;
r.run();
}
}
Presenter: Prem Chand Mali, Mindfire Solutions
Functional Interface
@FunctionalInterface
Public interface MyDemo {
public void doSomething();
public default String myDefault() {
System.out.println(“Hello world from default”);
}
}
Presenter: Prem Chand Mali, Mindfire Solutions
Method Reference
Presenter: Prem Chand Mali, Mindfire Solutions
• Method reference can be use to reuse the existing methods.
• You need to use :: for method reference.
integer.forEach(Example::doSomething);
Presenter: Prem Chand Mali, Mindfire Solutions
Question and
Answer
Thank you
Presenter: Prem Chand Mali, Mindfire Solutions
www.mindfiresolutions.com
https://www.facebook.com/MindfireSolutions
http://www.linkedin.com/company/mindfire-solutions
http://twitter.com/mindfires
Presenter: Prem Chand Mali, Mindfire Solutions

Java8 Lambda and Streams

  • 1.
    Introduction of Java8 LambdaExpression Presenter: Prem Chand Mali, Mindfire Solutions Date: 19/06/2014
  • 2.
    Presenter: Prem ChandMali, Mindfire Solutions About Me SCJP/OCJP - Oracle Certified Java Programmer MCP:70-480 - Specialist certification in HTML5 with JavaScript and CSS3 Exam Skills : Java, Swings, Springs, Hibernate, JavaFX, Jquery, prototypeJS, ExtJS. Connect Me : https://www.facebook.com/prem.c.mali http://www.linkedin.com/in/premmali https://twitter.com/prem_mali https://plus.google.com/106150245941317924019/about/p/pub Contact Me : premchandm@mindfiresolutions.com / prem.c.mali@gmail.com mfsi_premchandm
  • 3.
    Agenda Presenter: Prem ChandMali, Mindfire Solutions History What is Lambada ? Syntax Function Interface Default Methods Variable capture Method References Q & A
  • 4.
    History • Lambda calculus(also written as λ-calculus) is a formal system in mathematical logic and computer science for expressing computation based on function abstraction and application using variable binding and substitution. • The lambda calculus was introduced by mathematician Alonzo Church in the 1930s as part of an investigation into the foundations of mathematics. • Programming languages ( C#, Javascript, Ruby, C++, etc) have support for lambda expressions. • Java 8 have lambda expressions. Presenter: Prem Chand Mali, Mindfire Solutions
  • 5.
    What is Lamdbaexpression ? • Lambda expressions are anonymous functions. • Lambda expressions can be assigned to variable, passed to function and it can be returned from function. Presenter: Prem Chand Mali, Mindfire Solutions
  • 6.
    Syntax () -> {System.out.printlns("Hello World!");} (int a, int b) -> a + b () -> { return 1; } (String name) -> { System.out.println("Hello "+name); } n -> n % 2 != 0 Presenter: Prem Chand Mali, Mindfire Solutions
  • 7.
    JDK single methodinterfaces public interface Runnable { void run(); } public interface Callable<V> { V call() throws Exception; } public interface ActionListener { void actionPerformed(ActionEvent e); } public interface Comparator<T> { int compare(T o1, T o2); boolean equals(Object obj); } Presenter: Prem Chand Mali, Mindfire Solutions
  • 8.
    Functional Interface • FunctionalInterfaces Annotate by @FunctionInterface • Functional Interfaces which has only one method. • Functional Interfaces can have one or more default methods. Presenter: Prem Chand Mali, Mindfire Solutions
  • 9.
    Functional Interface @FunctionalInterface Public interfaceMyDemo { public void doSomething(); //public void doSomethingForDemo(); Compiler error. } Presenter: Prem Chand Mali, Mindfire Solutions
  • 10.
    Default Methods • Defaultmethod enable you to add new functionality to the interfaces of your libraries and ensure binary compatibility with code written for older versions of those interfaces. • Default key word needs to use to make default method. • Default method shouldn't be abstract. Presenter: Prem Chand Mali, Mindfire Solutions
  • 11.
    Default Methods @FunctionalInterface Public interfaceMyDemo { public void doSomething(); public default String myDefault() { System.out.println(“Hello world from default”); } } Presenter: Prem Chand Mali, Mindfire Solutions
  • 12.
    Variable Capture • InJava8 Local variable are accessible from Lambda expressions. In Java7 compiler complains. • When you try to modify local variable value compiler will complain if it not final. Presenter: Prem Chand Mali, Mindfire Solutions
  • 13.
    Variable Capture Public ClassMyDemo { public static void main(String[] args) { String myName = “Runnable”; Runnable r = () → System.out.println(“ Name : ” + myName); //myName = “something”; r.run(); } } Presenter: Prem Chand Mali, Mindfire Solutions
  • 14.
    Functional Interface @FunctionalInterface Public interfaceMyDemo { public void doSomething(); public default String myDefault() { System.out.println(“Hello world from default”); } } Presenter: Prem Chand Mali, Mindfire Solutions
  • 15.
    Method Reference Presenter: PremChand Mali, Mindfire Solutions • Method reference can be use to reuse the existing methods. • You need to use :: for method reference. integer.forEach(Example::doSomething);
  • 16.
    Presenter: Prem ChandMali, Mindfire Solutions Question and Answer
  • 17.
    Thank you Presenter: PremChand Mali, Mindfire Solutions
  • 18.