java8 lambda and streams

Download Java8 Lambda and Streams

If you can't read please download the document

Upload: mindfire-solutions

Post on 14-Jun-2015

253 views

Category:

Software


4 download

DESCRIPTION

Lambda expressions are a new and important feature that has been included in Java SE 8. It provides a way to represent one method interface using an expression. A lambda expression is like a method, it provides a list of formal parameters and a body (which can be an expression or a block of code) expressed in terms of those parameters.

TRANSCRIPT

  • 1. Introduction of Java8 Lambda Expression Presenter: Prem Chand Mali, Mindfire Solutions Date: 19/06/2014

2. 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 : [email protected] / [email protected] mfsi_premchandm 3. Agenda Presenter: Prem Chand Mali, 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 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 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 method interfaces public interface Runnable { void run(); } public interface Callable { V call() throws Exception; } public interface ActionListener { void actionPerformed(ActionEvent e); } public interface Comparator { int compare(T o1, T o2); boolean equals(Object obj); } Presenter: Prem Chand Mali, Mindfire Solutions 8. 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 9. Functional Interface @FunctionalInterface Public interface MyDemo { public void doSomething(); //public void doSomethingForDemo(); Compiler error. } Presenter: Prem Chand Mali, Mindfire Solutions 10. 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 11. 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 12. 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 13. 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 14. 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 15. 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); 16. Presenter: Prem Chand Mali, Mindfire Solutions Question and Answer 17. Thank you Presenter: Prem Chand Mali, Mindfire Solutions 18. 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