What is lambda
- Functional programming? Anonymous inner classes? Concise?
- λ comes from the academic circle, mainly parameterized behavior, simplified inner class, widely used in functional interface.
2. Why lambda?
- Don’t ask me why. The director is my cousin.
- In this article, you’ll learn to use lambda once and for all, and you’ll be able to () ->{} wherever you go.
3. Lambda syntax
- A complete lambda expression
(Type1 param1,Type2 param2,Type3 params)->{ statement1; statement2; .return data;
}
Copy the code
1, ()->{}, lambda parameters, arrows, lambda body is necessary
Parameter types can be omitted, and almost all of them are omitted
3. If there is only one statement in the body of the lambda, the braces, return, and trailing semicolon can be omitted
Method references can be used as lambda expressions
No arguments lambda
- Advanced over anonymous inner classes, interface names and function names are omitted.
- The lambda body has only one line and {} can be omitted.
Have the cords lambda
- A lot of times IDEA can be optimized, but but… The submitted codebase is still gray with idea prompts.
Functional interface
- The basis for lambda is functional interfaces, that is, interfaces that have only one abstract method inside them, not just one method.
- Type inference for lambda expressions is also related to functional interfaces, where the compiler can infer parameter types from context information.
Several typical functional interfaces
1, BiFunction
@FunctionalInterface
public interface BiFunction<T.U.R> {
R apply(T t, U u);
default <V> BiFunction<T, U, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return(T t, U u) -> after.apply(apply(t, u)); }}Copy the code
1. This functional interface is used heavily in Java 8 Stream.
2. There are two methods and only one abstract method
3. The abstract method apply takes two arguments of type T and U and returns R.
The BiFunction function interface of the third parameter of get () below, so you can pass a lambda that conforms to the function specification when you call get (). As for parameter 1, parameter 2 how to operate, operation, encryption… Depends on your lambda body.
2, Consumer
- Consume a function that returns void.
@FunctionalInterface
public interface Consumer<T> {
void accept(T t);
default Consumer<T> andThen(Consumer<? super T> after) {
Objects.requireNonNull(after);
return(T t) -> { accept(t); after.accept(t); }; }}Copy the code
- Returns the value void, so its side effects are generally used.
3, Predicate
- Predicate/condition test, define an abstract method, must have 1 input parameter, return value Boolean.
- Filter (), most commonly used in a stream, can form a chain of predicate calls.
// All of the following can be made a lambda of the Predicate
1, (s) - > s.l ength () >0;
2And (v) - > > v100;
3, (o) - > grown tartWith ("a");
Copy the code
4: Supplier,
- No input, output T. Like a container, call GET returns an object.
Supplier<Integer> supplier = new Supplier<Integer>() {
@Override
public Integer get(a) {
// Returns a random value
return newRandom().nextInt(); }}; System.out.println(supplier.get()); System.out.println(supplier.get());Copy the code
Method references
- The simple idea is that you can reuse existing method definitions and pass them like a lambda, represented by ::. There are three main categories.
- Core point: The signature of the function conforms to that of the parameter type, the number of parameters, the return value type or void.
Method references for static methods
- Static methods act on objects.
public static void main(String[] args) {
List<String> list = Lists.newArrayList("1"."2"."3"."4");
List<Integer> collect1 = list.stream().map(v -> Integer.parseInt(v))
.collect(Collectors.toList());
List<Integer> collect2 = list.stream().map(Integer::parseInt)
.collect(Collectors.toList());
}
Copy the code
Method references to instance methods of any type
1. This arbitrary type generally refers to the input type of your lambda.
2, the following input parameter is v, call v.length can be written as v String::length.
public static void main(String[] args) {
List<String> list = Lists
.newArrayList("hello"."how"."are"."you"."i"."am"."fine"."thank"."you"."and"."you");
Set<Integer> collect = list.stream().map(v -> v.length()).collect(Collectors.toSet());
Set<Integer> collect1 = list.stream().map(String::length).collect(Collectors.toSet());
System.out.println(collect);
System.out.println(collect1);
}
Copy the code
3. Method references to methods of existing objects
- Similar to point 2 above, not detailed.
Five, the summary
- Lambda expressions and functional interfaces are mainly introduced
- The core of using functional interfaces is to get the function signature right, pass in a lambda that matches the conditional signature, and you can ()->{}.