First, what is the lambda
- Functional Programming? Anonymous inner classes? concise?
- [Lambda] from academia, mainly for the actions of the simplified internal class, widely used in the functional interface.
Second, why should learn lambda?
- Do not ask me why, the director is my cousin.
- This article allows you to thoroughly learn to use lambda, wherever you can () -> {} a winded.
Three, lambda syntax
- A complete lambda expression
(Type1 param1,Type2 param2,Type3 params)->{
statement1;
statement2;
....
return data;
}
复制代码
1, () -> {}, lambda parameter, arrows, lambda body indispensable
2, parameter types may be omitted, and are basically omitted
3, lambda body braces, return and end with a semicolon if only one statement can be omitted
4, lambda expressions can be used as a reference method
No-argument lambda
- More advanced than anonymous inner classes, interfaces names, function names are omitted.
- lambda body is only one line, {} may be omitted.
There are parameters lambda
- In many cases idea can be optimized, but however. . . Submitted code base is still a pile of gray idea prompted.
Fourth, the function interface
- Lambda can be used is based on a function interface, i.e. the internal interface is only an abstract method, not only a method.
- lambda expression type inference is related with the interface function, the compiler can infer context information according to the parameter type.
Some typical interface functions
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));
}
}
复制代码
1, this function is widely used in the interface in java 8 stream.
2, there are two methods, only an abstract method
3, apply abstract method accepts two parameters, a type T, U, the return value is R.
4, following the get () function of the third parameter BiFunction interface, so you call the get () function can pass a line with the interface specifications lambda. For parameter 1, parameter 2 how to operate, operation, encryption. . . Lambda depends on your body.
2、Consumer
- Consumption function, the return value is 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); };
}
}
复制代码
- Return Value void, it is commonly used side-effects.
3、Predicate
- Assertion / test condition, define an abstract method, there must be a reference the value returned boolean.
- The most commonly used in the stream in the filter (), can be composed of predicate call chain.
//以下都可以做一个Predicate的lambda
1、(s)->s.length()>0;
2、(v)->v > 100;
3、(o)->o.startWith("a");
复制代码
4、Supplier
- It does not require input, output T. Like a container, calls get returned object.
Supplier<Integer> supplier = new Supplier<Integer>() {
@Override
public Integer get() {
//返回一个随机值
return new Random().nextInt();
}
};
System.out.println(supplier.get());
System.out.println(supplier.get());
复制代码
Method references
- Simply put, that is, so that you can re-use existing defined method, passing them as lambda, the use :: to represent. There are three types.
- The core point: the function signature to comply with that type of mass participation, the number of parameters, return type is void.
1, a reference method static method
- Static method acting on the object.
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());
}
复制代码
2, any type of pointing method reference example of a method
1, this generally refers to any type of lambda into your argument types.
2, as the following to the Senate is v, v can be written to call v.length type 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);
}
复制代码
3, point to methods of both object references
- Similarly to the first two points, not described in detail.
V. Summary
- Introduces the lambda expression and function interface
- Using the core function of the interface is to engage in the function signature, pass a qualifying signature of lambda, you can () -> {a} instigate.