Lamda expression learning note 1

Lamda expression learning note 1

1, Grammatical interpretation of Lamda

One of the three silly Bollywood heroes, lancher, said something that impressed me deeply: express the same meaning in simple language

I'm not saying how wrong the definition in the book is, but we should understand the definition in the book and describe it in simple words!

So what's the lamda expression?

Definition: a lamda expression is a transitive block of code that can be executed one or more times later (transferring code like data).

Transitive code block? Anonymous inner class is a kind of code block!

 1 /**
 2      * Normal anonymous function
 3      */
 4     @Test
 5     public void test() {
 6         Comparator<Integer> comparator = new Comparator<Integer>() {
 7             @Override
 8             public int compare(Integer x, Integer y) {
 9                 return Integer.compare(x, y);
10             }
11         };
12         TreeSet<Integer> ts = new TreeSet<>(comparator);
13     }
14 
15     /**
16      * lamda Expression
17      */
18     @Test
19     public void test1() {
20         Comparator<Integer> comparator = (x, y) -> Integer.compare(x, y);
21         TreeSet<Integer> ts = new TreeSet<>(comparator);
22     }

It is proved that lamda syntax can replace anonymous function, which means that lamda expression needs the support of interface.

 

What kind of interface is needed to support lamda expression? Let's talk about this later.

 

2, Lamda syntax

They are

  1. parameter list
  2. lamda symbol
  3. lamda body, implementation of interface abstract method

The parameter list here is the parameter of the abstract method in the interface, and the lamda body is the implementation of the abstract method in the interface. Which method in the interface should we use to write the lamda expression? The jvm doesn't know about this problem, so we need an interface with only one abstract method

Functional interface: a function with only one abstract method, usually declared as @ FunctionInterface

 

3, The representation of lamda expression

Four forms of lamda expression

  1. No parameter - > no return value

/**
     * No parameter - > no return value
     */
    @Test
    public void test2() {
        Runnable runnable = () -> System.out.println("hahahah");
        runnable.run();
    }

2. No parameter - > return value

/**
     * No parameter - > return value
     */
    @Test
    public void test3() {
        Supplier<Integer> consumer = () -> 10;
        Integer o = consumer.get();
        System.out.println(o);
    }

 

3. With parameter - > no return value

/**
     * With parameter - > no return value
     */
    @Test
    public void test4() {
        Consumer consumer = (x) -> System.out.println();
        consumer.accept("hahahah");
    }

 

4. Parameter - return value

/**
     * With parameter - > with return value
     */
    @Test
    public void test5() {
        Function<Integer, Integer> function = (x) -> x * x;
        System.out.println(function.apply(1000));
    }

 

4, Four built-in functional interfaces

The four interfaces are: consumer < T >, supplier < T >, function < T, R >, predicate < T >

1. Consumer < T > consumer interface

@FunctionalInterface
public interface Consumer<T> {
    void accept(T var1);

    default Consumer<T> andThen(Consumer<? super T> after) {
        Objects.requireNonNull(after);
        return (t) -> {
            this.accept(t);
            after.accept(t);
        };
    }
}

 

2. Supplier < T > supply interface

package java.util.function;

@FunctionalInterface
public interface Supplier<T> {
    T get();
}

 

3. Function < T, R > functional interface

import java.util.Objects;

@FunctionalInterface
public interface Function<T, R> {
    R apply(T var1);

    default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
        Objects.requireNonNull(before);
        return (v) -> {
            return this.apply(before.apply(v));
        };
    }

    default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
        Objects.requireNonNull(after);
        return (t) -> {
            return after.apply(this.apply(t));
        };
    }

    static <T> Function<T, T> identity() {
        return (t) -> {
            return t;
        };
    }
}

 

4. Predicate < T > assertion interface (function return value is boolean value)

package java.util.function;

import java.util.Objects;

@FunctionalInterface
public interface Predicate<T> {
    boolean test(T var1);

    default Predicate<T> and(Predicate<? super T> other) {
        Objects.requireNonNull(other);
        return (t) -> {
            return this.test(t) && other.test(t);
        };
    }

    default Predicate<T> negate() {
        return (t) -> {
            return !this.test(t);
        };
    }

    default Predicate<T> or(Predicate<? super T> other) {
        Objects.requireNonNull(other);
        return (t) -> {
            return this.test(t) || other.test(t);
        };
    }

    static <T> Predicate<T> isEqual(Object targetRef) {
        return null == targetRef ? Objects::isNull : (object) -> {
            return targetRef.equals(object);
        };
    }

    static <T> Predicate<T> not(Predicate<? super T> target) {
        Objects.requireNonNull(target);
        return target.negate();
    }
}

 

There are not only these 4 functional interfaces, but also java8 provides us with

 

At last, there are three parts in lamda expression that can be optimized

1. When there is only one parameter, you can not write parentheses

2. When there is only one statement in the body of lamda, you can use no braces

3. Parameter list does not need to declare parameter types. The jvm will analyze data types according to the previous and next articles (no need to consider performance issues, because no matter how our. java code compiles)

The article is only for communication and study. I hope you can forgive me for writing the article for the first time

Reference resources:  https://www.bilibili.com/video/av62117143

Tags: Java jvm

Posted on Fri, 07 Feb 2020 03:56:08 -0800 by Haggen