Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

Java 8 - Lambda Expressions

Ben

Developer - JS
Developer
Joined
Jul 6, 2013
Messages
1,224
Reaction score
506
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

Ben - Java 8 - Lambda Expressions - RaGEZONE Forums


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:
Experienced Elementalist
Joined
Mar 12, 2015
Messages
238
Reaction score
43
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
 

Ben

Developer - JS
Developer
Joined
Jul 6, 2013
Messages
1,224
Reaction score
506
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.
 
Divine Celestial
Joined
Feb 25, 2013
Messages
808
Reaction score
343
Take a look here if you are interested into performance:

 
not a programmer
Joined
Mar 30, 2015
Messages
532
Reaction score
62
 
Joined
Jun 8, 2007
Messages
1,985
Reaction score
490
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.
 
Back
Top