Lambda expressions notes

Source: Internet
Author: User

With the release of Java 8, "the Java (TM) Tutorials" added a tutorial on "lambda expressions. This article is a summary and summary of this tutorial.

 

1. Lambda is essentially an anonymous class

2. Lambda formulas can only implement functional interfaces;
1. There is only one virtual method excuse, called the functional interface;
2. There can be one or more default and static methods in the functional interface.

Iii. Lambda formula format

For example:

p -> p.getGender() == Person.Sex.MALE    && p.getAge() >= 18    && p.getAge() <= 25

1. "->" indicates the Lambda formula, the parameter table on the left, and the method body on the right;

2. parameter table
A. a Lambda parameter table is similar to a method parameter table, which is enclosed in parentheses and separated by commas. Example: (P, Q, R );
B. parameters can have but do not have to have type modification. The compiler will determine the parameter type by itself;
C. Only one parameter can omit the arc;
D. If the corresponding method has no parameters, write the lambda parameter table as "()-> ".

3. Method body
A. Same as the method body of the general method;
B. If there are multiple statements, use the {} package;
C. If there are multiple statements and the method requires the return result, return is used for the last statement.


4. Java 8 pre-defines some functionl interfaces in Java. util. function, which are roughly as follows:

@FunctionalInterfacepublic 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); };    }}@FunctionalInterfacepublic interface Predicate<T> {    boolean test(T t);    default Predicate<T> and(Predicate<? super T> other) {        Objects.requireNonNull(other);        return (t) -> test(t) && other.test(t);    }    default Predicate<T> negate() {        return (t) -> !test(t);    }    default Predicate<T> or(Predicate<? super T> other) {        Objects.requireNonNull(other);        return (t) -> test(t) || other.test(t);    }    static <T> Predicate<T> isEqual(Object targetRef) {        return (null == targetRef)            ? Objects::isNull            : object -> targetRef.equals(object);    }}@FunctionalInterfacepublic interface Function<T, R> {    R apply(T t);    default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {        Objects.requireNonNull(before);        return (V v) -> apply(before.apply(v));    }    default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {        Objects.requireNonNull(after);        return (T t) -> after.apply(apply(t));    }    static <T> Function<T, T> identity() {        return t -> t;    }}

 

5. Aggregate operations can be implemented by using the streams and pipelines defined in the Java. util. Stream package, such:

roster.stream().filter(    p -> p.getGender() == Person.Sex.MALE    && p.getAge() >= 18    && p.getAge() <= 25).map(p -> p.getEmailAddress()).forEach(email -> System.out.println(email));

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.