并发编程整理笔记09-四大函数式接口

四大函数式接口(重点)

新特性:lambda表达式、链式编程、函数式接口、Stream流式计算

函数式接口:只有一个方法的接口
在新版本的框架中大量使用,可简化编程模型

@FunctionalInterface
public interface Runnable {
    public abstract void run();
}
  • 四大原生函数式接口
    在这里插入图片描述
  • 代码测试:

函数式接口
Function

@FunctionalInterface
public interface Function<T, R> {

    /**
     * Applies this function to the given argument.
     *
     * @param t the function argument
     * @return the function result
     */
    R apply(T t);

    /**
     * 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)
     */
    default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
        Objects.requireNonNull(before);
        return (V v) -> apply(before.apply(v));
    }

    /**
     * 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)
     */
    default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
        Objects.requireNonNull(after);
        return (T t) -> after.apply(apply(t));
    }

    /**
     * Returns a function that always returns its input argument.
     *
     * @param <T> the type of the input and output objects to the function
     * @return a function that always returns its input argument
     */
    static <T> Function<T, T> identity() {
        return t -> t;
    }
}

  • 函数式接口:有一个输入参数,有一个输出参数
  • 只要是函数式接口,都可以用lambda来表示
public class FunctionTest {
    public static void main(String[] args) {
        // 接口利用匿名内部类创建
        Function function = new Function<String,String>() {
            @Override
            public String apply(String s) {
                return s;
            }
        };
        // 利用函数式接口替代匿名内部类
        Function<String,String> function1 = (str)->{return str;};

        System.out.println(function.apply("abc"));
    }
}

断定型接口:Predicate

  • 是断定型接口:有一个输入参数,返回值只能是布尔值
public class PredicateTest {
    public static void main(String[] args) {
        // 匿名内部类
        Predicate<String> predicate = new Predicate<String>() {
            @Override
            public boolean test(String s) {
                return false;
            }
        };
        // lambda表达式简化
        Predicate predicate1 = (str)->{
            return str.equals("123");
        };
    }
}

消费型接口:Consumer

  • 只有输入,没有返回值
public class ConsumerTest {
    public static void main(String[] args) {

        Consumer<String> consumer = new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println("消费型接口");
            }
        };

        Consumer<String> consumer1 = (str) -> {
            System.out.println("消费型接口lambda表达式");
        };
    }
}

供给型接口:

  • 没有输入,只有返回值
public class SupplierTest {

    public static void main(String[] args) {
        Supplier supplier = new Supplier<Integer>() {
            @Override
            public Integer get() {
                return null;
            }
        };

        Supplier supplier1 = ()->{return 123;};
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值