- 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:
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:
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:
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:
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:
To apply this interface into our code we do the following:
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:
(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!
-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();
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: