Illustrates jdk1.8 new features (2) --- Lambda

Illustrates jdk1.8 new features (2) --- Lambda

A brief description


Common interface function jdk

Predicate

@FunctionalInterface
public 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);
    }
}
  • predicateInterface returns the results of a test, the result type is true or false
  • predicateSupports and, or, negate method

Consumer

@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); };
    }
}
  • consumerInterface accepts a parameter, and then do the consumption parameters and returns void

  • consumerTo support chained method calls consumption by andThen


Function

@FunctionalInterface
public 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;
    }
}
  • FunctionThe interface can be seen as a factory, the raw material receiving output obtained

  • FunctionInterface may be invoked by a front compose method, by the post andThen method call, passing parameters obtained by the method of identity
  • Note that compose methods and andThen method parameters used in the definition of <? Super X> and <? Extends X>, method used to fully meet the principles of PECS (produce extends consume super)


Supplier

@FunctionalInterface
public interface Supplier<T> {
    T get();
}
  • supplier Completely out of thin air is a creator, does not require any input, output is expected to return directly

The Lambda leaner

  • In most cases a lambda expression can uniquely determine the type of parameters, so you can just write the parameter type parameter name is omitted
  • When the expression of lambda expressions contain only one statement when the braces can be omitted semicolons, statements, and return the end of the sentence
  • lambda expression can aforesaid method further simplified form of a reference

Guess you like

Origin www.cnblogs.com/Kidezyq/p/11728110.html