I struggled with the lambda expression while studying Oracle Silver, so I will write down what I understood.
You can treat the method like a variable. You can pass a method as a method argument. You can write the same thing as an anonymous class with a simpler description.
You need to override the abstract method of the functional interface.
(Type argument name)-> {Process 1;};
(Object o) -> {System.out.println(o);};
Lambda expressions are ** actually just overriding abstract methods **. To understand it ・ Override normally -Override with anonymous class -Override with lambda expression I will write with three.
This time, we will override and implement the abstract method accept of the functional interface Consumer.
Consumer interface
@FunctionalInterface
public interface Consumer<T> {
void accept(T t);
//...
}
The first is to implement the Consumer interface normally with "implements" and override the accept method.
Ordinary implementation
import java.util.function.Consumer;
public class Lambda2 implements Consumer<String> {
@Override
public void accept(String s) {
System.out.println(s);
}
public static void main(String[] args) {
Lambda2 lam2 = new Lambda2();
lam2.accept("usually");
}
}
Next is the anonymous class. The point is @Override.
Implemented in anonymous class
import java.util.function.Consumer;
public class lambda3 {
public static void main(String[] args) {
Consumer<String> func = new Consumer<String>() {
@Override
public void accept(String s) {
System.out.println(s);
}
};
func.accept("Anonymous class");
}
}
Finally, how to implement a Consumer interface with a lambda expression. Overriding the accept method with a lambda expression.
Implemented by lambda expression
import java.util.function.Consumer;
public class Lambda1 {
public static void main(String[] args) {
Consumer<String> func = (s) -> {System.out.println(s);};
func.accept("Lambda expression");
//Abbreviation notation. Same process as above.
Consumer<String> func2 = s -> System.out.println(s);
func.accept("Lambda abbreviation");
}
}
All you're doing is ** overriding the accept method ** for all normal, anonymous classes, and lambda expressions.
Consumer <String> func = (s)-> {System.out.println (s);};
func.accept ("lambda expression");
Using a lambda expression with the Stream API seems to be the strongest, so I'll try it.
As before, the order is as follows. ・ Implemented normally -Implemented by anonymous class ・ Implemented by lambda expression
Consumer interface
@FunctionalInterface
public interface Consumer<T> {
void accept(T t);
//...
}
usually
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
public class Normal implements Consumer<String>{
@Override
public void accept(String s) {
System.out.println(s);
}
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("1");
list.add("2");
list.add("3");
//Extract the contents of list with forEach.
Normal n = new Normal();
list.forEach(n);//Consumer as an argument for forEach<String>Pass the mold
}
}
Anonymous
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
public class Tokumei {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("1");
list.add("2");
list.add("3");
list.forEach(new Consumer<String>() {
@Override
public void accept(String s) {
System.out.println(s);
}
});
}
}
Lambda expression
import java.util.ArrayList;
import java.util.List;
public class Lambda4 {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("1");
list.add("2");
list.add("3");
list.forEach(s -> System.out.println(s));
}
}
A lambda expression requires a very short description! It's pretty easy because you don't have to write implements or @Override.
forEach takes a Consumer
forEach(Consumer<T> t)
The accept method is included in this argument and is passed to forEach. And it is an image that the accept method is repeatedly executed in forEach. This is a convenient point for lambda expressions that pass a method as a method argument.
You can write the functional interface yourself, but the ones you use most are already available.
Consumer It just receives an argument and has no return value. Argument type: T
@FunctionalInterface
public interface Consumer<T> {
void accept(T t);
//...
}
Function Takes the argument T and returns R. Argument type: T Return type: R
@FunctionalInterface
public interface Function<T, R> {
R apply(T t);
//...
}
Predicate Takes the argument T and returns a boolean. Argument type: T Return type: boolean
@FunctionalInterface
public interface Predicate<T> {
boolean test(T t);
//...
}
Supplier Returns T without receiving any arguments. Return type: T
@FunctionalInterface
public interface Supplier<T> {
T get();
//...
}
I found that using a lambda expression can significantly reduce the description. It seems that you can get used to it by rewriting it as normal → anonymous class → lambda expression. I will actively use it when using arrays and collections.
Recommended Posts