Java, which is used for large-scale development, requires that the amount of code be reduced as much as possible and that it be easy to read. The "lambda expression" used in Java 8 is useful for such occasions. A feature of lambda expressions is that you can treat methods like variables.
How to write:
[Interface declaration]= (argument)->{Execution block}
Let's check it from the source code at once. First of all, the traditional expression:
public class LambdaTest {
public static void main(String[] args) {
//Traditional expression
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("threading" + Thread.currentThread().getId());
}
}).start();
}
}
The following is a representation of a Lambda expression.
public class LambdaTest {
public static void main(String[] args) {
//Lambda expression
new Thread(() -> {
System.out.println("Lambda threading" + Thread.currentThread().getId());
}).start();
}
}
Java's functional interface is almost the same as a traditional interface, but the only feature is that the functional interface is "an interface with only one abstract method defined". Static methods and default methods can be included. (It's a good idea to annotate java.lang.FunctionalInterface).
See the detailed introduction to the Java function interface. https://qiita.com/Jenny1025/items/470e304ff77c44874fc8
Example:
@FunctionalInterface
public interface UserIdentify {
String verifyUser(String userName);
}
The advantage of Java's Lambda expression is that it can simplify the description of functional interfaces using anonymous classes.
Anonymous class:
public class App
{
public static void main( String[] args ) {
//Functional interface
UserIdentify ic = new UserIdentify() {
@Override
public String verifyUser(String userName) {
return "admin".equals(userName)? "admin":"member";
}
};
System.out.println(ic.verifyUser("admin"));
System.out.println(ic.verifyUser("manager"));
}
}
So how do you express it in a Lambda expression?
public class App {
public static void main( String[] args ) {
UserIdentify ic1 = (String userName) -> {
return "admin".equals(userName)? "admin":"member";
};
System.out.println(ic1.verifyUser("admin"));
System.out.println(ic1.verifyUser("manager"));
}
}
Let's start with a concrete example.
public class App {
public static void main( String[] args ) {
//No arguments, no return value
Lambda1 lambda1 = () -> System.out.println("Hello world");
lambda1.test();
}
interface Lambda1 {
void test();
}
}
Implementation result: Hello world
public class App {
public static void main( String[] args ) {
//With arguments, no return value
Lambda2 lambda2 = (n, a) -> System.out.println(n + " is " + a + " years old");
lambda2.test("Sally",18);
}
interface Lambda2 {
void test(String name, Integer age);
}
}
Implementation result: Sally is 18 years old
public class App {
public static void main( String[] args ) {
//With arguments, with return value
Lambda3 lambda3 = (a, b) -> a + b;
System.out.println(lambda3.test(100,3000));
}
interface Lambda3 {
int test(int a, int b);
}
}
Implementation result: 3100
Recommended Posts