Java 8 - Lambda Expressions

Results 1 to 7 of 7
  1. #1
    C# - Java - Notepad Ben is offline
    ModeratorRank
    Jul 2013 Join Date
    BelguimLocation
    1,229Posts

    Java 8 - Lambda Expressions


    RaGEZONE Recommends

    RaGEZONE Recommends

    For this guide you should atleast know:


    -basic Java
    -How to work with interfaces


    To start with, lets do a small recap of some basic interface within java


    In the old way, before Java 8, you had to implement interfaces using Annonymous classes.


    For example we have the interface DisplayInterface that contains 1 method called Display


    So we would create a new interface:


    Code:
    public interface DisplayInterface
    {
        void Display();
    }


    And afterwards, in the main we would need to use an annonymous inner class and implement the Display method in there.


    so in our main, we would do:


    Code:
    public static void main(String[] args) {
            
            //old way to implement an interface
            FunctionalInterfaceTest fit = new FunctionalInterfaceTest() {
                
                @Override
                public void Display() {
                    System.out.println("Display from the old way");
                    
                }
            };
            fit.Display();

    With lambda expressions, we can provide the exact same result with less code and compacter written then by doing the full implementation within the class.


    So when we write the lambda expression, we write:


    Code:
    FunctionalInterfaceTest newway = () -> { System.out.println("Display from new lambda expression");};
            newway.Display();
    Now, lets go a bit deeper in what this "Lambda-expression" is made of and how we create them.


    Well, first to know is that a lambda expession makes use of Functional Interfaces.


    A functional Interface is an interface with a single (1) abstract method in it.


    For example, you want to create an interface that can subtract and add 2 values at eachother you would write:


    Code:
    interface MathInterface
    {
        public int subInterface(int num1, int num2);
        public int addInterface(int num1, int num2);
    }
    You will see that when you try to use those in a lambda expression or when you try to define that interface as a functional interface with the @ functionalInterface annotation, you will get an error as this interface contains multiple abstract methods which is not allowed for Functional interfaces & lambda expressions.


    The way to solve this is to make a functional interface for each of them alone.


    so we get:


    Code:
    interface SubtractInterface
    {
        public int Sub(int nm1, int num2); 
    }
    Code:
    interface AddInterface
    {
        public int add(int nm1, int num2);
    }
    To apply this interface into our code we do the following:


    Code:
    public static void main(String[] args)
    {
        AddInterface addInt = (Parameter1,Parameter2) -> { return Parameter1 + Parameter2;};
        
        System.out.println(addInt.add(10,20));
    }
    You can remove the {} in the lambda statement if your lambda expression is a single line statement.


    Lets split out the syntax of a lambda expression:


    Code:
    (Parameter1,Parameter2) -> { return Parameter1 + Parameter2;};
    (Parameter1, Parameter2) these are the arguments that you are using in the lambda expression, as you see it's not required to add any datatype as the lambda expression will look at the functional interface which types of parameters it has to receive.


    Another example of parameters written in a lambda expression can be:


    () Here the method does not require any parameters, imagine it as a void method that takes no parameters.


    The -> sign within the lambda expression has no value to the lambda, it only splits the parameter part of the lambda from the body part.


    Everything behind the -> is considered as the body of the lambda expression and can or can not return a value back depending on the return type of the function.

    Here's an example of my main in eclipse



    If theres anything not correct please feel free to comment, i'll be glad to fix any errors out of this guide.

    Cheers!
    Last edited by Ben; 07-12-15 at 04:48 PM.


  2. #2
    Account Upgraded | Title Enabled! Las Systos is offline
    True MemberRank
    Mar 2015 Join Date
    NetherlandsLocation
    211Posts

    Re: Java 8 - Lambda Expressions

    Thanks a lot, I actually had to send my teacher my project and accidently converted a for loop to lambda expression cause netbeans was giving it a warning, I forgot to set it back and send it to my teacher and he had some questions for me since I'm still at year 1 of my study. This helped me talk my way out of it :D

  3. #3
    Death from above! The General is offline
    The OmegaRank
    Aug 2011 Join Date
    8,944Posts

    Re: Java 8 - Lambda Expressions

    Could you bother benchmarking the 'regular' and the 'lambda' way? Just out of curiosity.
    If you are using Arcturus, contact me
    Skype: wesley.jabbo
    Discord: TheGeneral#0063


  4. #4
    C# - Java - Notepad Ben is offline
    ModeratorRank
    Jul 2013 Join Date
    BelguimLocation
    1,229Posts

    Re: Java 8 - Lambda Expressions

    Lamda is slower if i recall right, as it has to fall back to it determinating which types are passed in the method by looking at the functional interface.

  5. #5
    Account Upgraded | Title Enabled! InCube is offline
    True MemberRank
    Feb 2013 Join Date
    United KingdomLocation
    830Posts

    Re: Java 8 - Lambda Expressions


  6. #6
    total beginner eshays is offline
    True MemberRank
    Mar 2015 Join Date
    540Posts

    Re: Java 8 - Lambda Expressions


  7. #7
    :-) s-p-n is offline
    DeveloperRank
    Jun 2007 Join Date
    Next DoorLocation
    2,148Posts

    Re: Java 8 - Lambda Expressions

    It's interesting how functional languages try to become more like object oriented languages, and now the most famous object oriented language is trying to become more functional. I enjoy coding in both styles, and I always feel limited when I run into language oriented road-blocks. I'm all for this movement on both sides. Maybe the languages of the future will be even more similar to each other than they already are today. The downside may be compile/execution time. It seems reasonable to assume that a good specialized language should be more efficient in what it does than a jack-of-all-trades language.




Advertisement