java/函数式接口-function?

1 lambda依赖上下文

1.1 测试lambda表达式

public class Test3 {
    public static void main(String[] args) {
        TheInterface1 t1 = () -> {};
        TheInterface2 t2 = () -> {};
        System.out.println(t1.getClass().getInterfaces()[0]);
        System.out.println(t2.getClass().getInterfaces()[0]);
    }
}

@FunctionalInterface
interface TheInterface1{
    void myMethod1();
}

interface TheInterface2{
    void myMethod2();
}

lambda表达式依赖于上下文。是个对象。是函数式接口的实现类

1.2 结合Thread理解

开启一个线程

        new Thread(new Runnable() {
            @Override
            public void run() {

            }
        }).start();
        new Thread(() -> System.out.println("hello world")).start();

1.3 理解<>,菱形语法

Explicit(显示) type argument String can be replaced with <> 

 

1.4 理解Stream

/**
     * Returns a sequential(串形) {@code Stream} with this collection as its source.
     *
     * <p>This method should be overridden when the {@link #spliterator()}
     * method cannot return a spliterator that is {@code IMMUTABLE(并发的)},
     * {@code CONCURRENT(不可变的)}, or <em>late-binding(延迟绑定的)</em>. (See {@link #spliterator()}
     * for details.)
     *
     * @implSpec
     * The default implementation creates a sequential {@code Stream} from the
     * collection's {@code Spliterator}.
     *
     * @return a sequential {@code Stream} over the elements in this collection
     * @since 1.8
     */

同上:调用stream后,返回一个Stream对象

/**
     * Returns a stream consisting(组成) of the results of applying the given
     * function to the elements of this stream.
     *
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
     * operation(中间操作)</a>.
     *
     * @param <R> The element type of the new stream
     * @param mapper a <a href="package-summary.html#NonInterference">non-interfering(无干扰)</a>,
     *               <a href="package-summary.html#Statelessness">stateless(无状态)</a>
     *               function to apply to each element
     * @return the new stream
     */

/**
 * Represents(代表) a function that accepts one argument and produces a result.
 *
 * <p>This is a <a href="package-summary.html">functional interface</a>
 * whose functional method is {@link #apply(Object)}.
 *
 * @param <T> the type of the input to the function
 * @param <R> the type of the result of the function
 *
 * @since 1.8
 */

 

/**
     * Applies(应用) this function to the given argument.
     *
     * @param t the function argument
     * @return the function result
     */

 可以看到map的参数是Function,是个函数式接口。本就可以使用lambda/方法引用实现。可以点击上述标注的地方,确认具体实现的地方。。。

 

使用?String::toUpperCase是方法引用,类名直接引用共有方法,类名.共有方法不可行,因为方法不是静态方法,但是方法引用就可以

 

1.5 理解语句和表达式

 

 

 

2 lambda,传递的是一个行为

2.1 传递行为

 

2.2 两个function的串联

 

 

 

/**
     * Returns a composed(组合) function that first applies the {@code before}
     * function to its input, and then applies this function to the result.
     * If evaluation(计算) of either function throws an exception, it is relayed to
     * the caller of the composed function.
     *
     * @param <V> the type of input to the {@code before} function, and to the
     *           composed function
     * @param before the function to apply before this function is applied
     * @return a composed function that first applies the {@code before}
     * function and then applies this function
     * @throws NullPointerException if before is null
     *
     * @see #andThen(Function)
     */

/**
     * Returns a composed function that first applies this function to
     * its input, and then applies the {@code after} function to the result.
     * If evaluation of either function throws an exception, it is relayed to
     * the caller of the composed function.
     *
     * @param <V> the type of output of the {@code after} function, and of the
     *           composed function
     * @param after the function to apply after this function is applied
     * @return a composed function that first applies this function and then
     * applies the {@code after} function
     * @throws NullPointerException if after is null
     *
     * @see #compose(Function)
     */

2.3 Bifunction,输入两个参数,返回一个值

 

 

 

 

/**
 * Represents a function that accepts two arguments and produces a result.
 * This is the two-arity specialization(两个参数的特例) of {@link Function}.
 *
 * <p>This is a <a href="package-summary.html">functional interface</a>
 * whose functional method is {@link #apply(Object, Object)}.
 *
 * @param <T> the type of the first argument to the function
 * @param <U> the type of the second argument to the function
 * @param <R> the type of the result of the function
 *
 * @see Function
 * @since 1.8
 */

 

/**
     * Returns a composed function that first applies this function to
     * its input, and then applies the {@code after} function to the result.
     * If evaluation of either function throws an exception, it is relayed to
     * the caller of the composed function.
     *
     * @param <V> the type of output of the {@code after} function, and of the
     *           composed function
     * @param after the function to apply after this function is applied
     * @return a composed function that first applies this function and then
     * applies the {@code after} function
     * @throws NullPointerException if after is null
     */

 

 

2.4 Predict

 

 

 

/**
 * Represents a predicate (boolean-valued function) of one argument.
 *
 * <p>This is a <a href="package-summary.html">functional interface</a>
 * whose functional method is {@link #test(Object)}.
 *
 * @param <T> the type of the input to the predicate
 *
 * @since 1.8
 */

 

 

/**
     * Evaluates this predicate on the given argument.
     *
     * @param t the input argument
     * @return {@code true} if the input argument matches the predicate,
     * otherwise {@code false}
     */

 

 

  

  

 

posted @ 2021-02-20 13:54  谁给起个名  阅读(118)  评论(0)    收藏  举报