JDK8-函数式接口

函数式接口

函数式接口的由来

​ 我们知道使用Lambda表达式的前提是需要有函数式接口,而Lambda表达式使用时不关心接口名,抽象方法名。只关心抽象方法的参数列表和返回类型。

因此为了我们使用Lambda表达式更加的方法,在JDK中提供了大量常用的函数式接口。

public class Demo01Function {

    public static void main(String[] args) {

        fun1(arr -> {
            int sum = 0;
            for (int i : arr) {
                sum +=i;
            }
            return sum;
        });
    }

    public static void fun1(Operator operator){
        int[] arr ={1,2,3,4};
        int sum = operator.getSum(arr);
        System.out.println(sum);
    }
}

/**
 * 函数式接口
 */
@FunctionalInterface
interface Operator{
    int getSum(int[] arr);
}

必须始终使用@Functionallnterface 注解对自己编写的函数式接口进行标注

函数式接口介绍

接口函数签名范例
Perdiicateboolean test(T t)Collection::isEmpty
Function<T,R>R apply(T t)Arrays::asList
SupplierT get()Instant::now
ConsumerVoid accept(T t)System.out::println
UnaryOperatorT apply(T t)String::ToLowerCase
BinaryOperatorT apply(T t1,Tt2)BigInteger:add

​ 在JDK中帮我们提供了大量的函数式接口,主要是在java.util.function 包中。上面的这6个基础接口还有3种变体,分别可以作用基本类型 int、long 和 double。它们的命名方式是在基础接口饿前面加上基本类型而得。

​ Function 接口还有9种变体,用于结果类型为基本类型的情况。源类型和结果类型始终不一样,因为从类型到自身的函数就是 UnaryOperator。如果源类型和结果类型均为基本类型,就是在Function前面添加格式如SrcToResult,如 LongToIntFunction(有6种变体)。如果源类型为基本类型,结果类型是一个对象参数,则要在Function前面添加ToObj,如 DoubleToObjFunction(有三种变体)。

​ 这三种基础函数接口还有带两个参数的版本,如 BiPredocate<T,U>、BiFunction<T,U,R>和BiConsumer<T,U>。还有BiFunction变体用于返回三个相关的基本类型:ToIntBiFunction<T,U>、ToLongBiFunction<T,U>和ToDoubleBiFunction<T,U>。Consumer接口也有带两个参数的变体版本,它们带一个对象引用和一个基本类型:Obj-DoubleConsumer、ObjIntConsumer和ObjLongConsumer。

​ 最后,还有BooleanSupplier接口,它是Supplier接口的一种变体,返回boolean值。这是在所有的标准函数接口名称中唯一显示提到boolean类型的,但boolean返回值是通过Predicate及其4种变体来支持的。

1.supplier

无参有返回值的接口,需要提供一个返回类型

@FunctionalInterface
public interface Supplier<T> {

    /**
     * Gets a result.
     *
     * @return a result
     */
    T get();
}

使用示例:

public class SupplierTest {

    public static void main(String[] args) {

        fun1(() -> {
            //计算出数组中最大的值
            int[] arr = {22, 55, 66, 33, 12, 43, 35};
            Arrays.sort(arr);
            return arr[arr.length-1];
        });
    }

    public static void fun1(Supplier<Integer> supplier) {
        // get() 是一个无参的又返回值的 抽象方法
        Integer max = supplier.get();
        System.out.println("max = " + max);
    }
}
2.Consumer

有参无返回值的接口,使用的时候需要指定一个泛型来定义参数类型

@FunctionalInterface
public interface Consumer<T> {

    /**
     * Performs this operation on the given argument.
     *
     * @param t the input argument
     */
    void accept(T t);

    /**
     * Returns a composed {@code Consumer} that performs, in sequence, this
     * operation followed by the {@code after} operation. If performing either
     * operation throws an exception, it is relayed to the caller of the
     * composed operation.  If performing this operation throws an exception,
     * the {@code after} operation will not be performed.
     *
     * @param after the operation to perform after this operation
     * @return a composed {@code Consumer} that performs in sequence this
     * operation followed by the {@code after} operation
     * @throws NullPointerException if {@code after} is null
     */
    default Consumer<T> andThen(Consumer<? super T> after) {
        Objects.requireNonNull(after);
        return (T t) -> { accept(t); after.accept(t); };
    }
}
2.1 accept方法

使用示例:将输入的数据统一转换为小写输出

public class ConsumerTest {

    public static void main(String[] args) {
        test(msg -> {
            System.out.println(msg + "->转换为小写:" + msg.toLowerCase());
        });
    }

    public static void test(Consumer<String> consumer) {
        consumer.accept("Hello World");
    }
}
2.2 andThen默认方法

默认方法:andThen() 方法的参数和返回值全都是Consumer 类型,那么就可以实现效果:消费数据的时候,首先做一个操作,然后再做一个操作,实现组合。

public class ConsumerTest {

    public static void main(String[] args) {
        testAndThen(msg1 -> {
            System.out.println(msg1 + "->转换为小写:" + msg1.toLowerCase());
        },msg2->{
            System.out.println(msg2 + "->转换为小写:" + msg2.toUpperCase());
        });
    }

    public static void testAndThen(Consumer<String> con1,Consumer<String> con2){
        String str = "Hello Wolrd";
        con1.andThen(con2).accept(str);  // 先转换为小写,在转换为大写
        System.out.println("------------------");
        con2.andThen(con1).accept(str);  // 先转换为大写,在转换为小写
    }
}
3.function
@FunctionalInterface
public interface Function<T, R> {

    /**
     * Applies this function to the given argument.
     *
     * @param 输入一个 T 类型的参数
     * @return 返回一个 R 类型的值
     */
    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));
    }

    /**
     * 组合两个函数方法,返回一个新的函数
     * after 的 apply 方法输入一个 R 类型的参数,返回一个 V 类型
     * 当前的 Function 的 apply 方法是输入一个 T 类型的参数,返回一个 R 类型
     * 两个函数组合之后,返回一个新的 Funcation,新的Function输入一个 T 类型的参数,返回一个 V 类型
     * @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;
    }
}
3.1 apply方法

有参数又返回值的方法,Function接口是根据一个类型得到另一个类型的数据,前者称为前置条件,后者称为后置条件

public class FunctionTest {

    public static void main(String[] args) {
        fun1(msg -> {
            return Integer.parseInt(msg);
        });
    }

    /**
     * 输入一个String类型,返回一个Integer类型
     * @param function
     */
    public static void fun1(Function<String, Integer> function) {
        Integer apply = function.apply("666");
        System.out.println("apply = " + apply);
    }
}
3.2 andThen默认方法
public class FunctionTest {

    public static void main(String[] args) {
        fun2(msg -> Integer.parseInt(msg),msg2-> msg2 * 10);
    }

    /**
     * fun2中传入的参数必须是fun1返回的类型,也就是fun1返回什么类型,fun2就传入什么类型的参数
     * @param fun1 传入一个String类型,返回一个 Integer
     * @param fun2 传入一个Integer类型的参数,返回一个Integer类型
     */
    public static void fun2(Function<String,Integer> fun1,Function<Integer,Integer> fun2){
        Integer apply = fun1.andThen(fun2).apply("666");
        System.out.println("apply = " + apply);
    }
}
3.3 compose默认方法

compose方法正好更andThen方法执行顺序相反,

public class FunctionTest {

    public static void main(String[] args) {
        fun2(msg -> Integer.parseInt(msg),msg2-> msg2 * 10);
    }

    public static void fun2(Function<String,Integer> fun1,Function<Integer,Integer> fun2){
        Integer apply = fun2.andThen(fun1).apply("666");
        System.out.println("apply = " + apply);
    }
}
3.4 identity静态方法

identity静态方法是,输入什么类型的参数就返回什么类型

4.Predicate
@FunctionalInterface
public interface Predicate<T> {

    /**
     * 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}
     */
    boolean test(T t);

    /**
     * Returns a composed predicate that represents a short-circuiting logical
     * AND of this predicate and another.  When evaluating the composed
     * predicate, if this predicate is {@code false}, then the {@code other}
     * predicate is not evaluated.
     *
     * <p>Any exceptions thrown during evaluation of either predicate are relayed
     * to the caller; if evaluation of this predicate throws an exception, the
     * {@code other} predicate will not be evaluated.
     *
     * @param other a predicate that will be logically-ANDed with this
     *              predicate
     * @return a composed predicate that represents the short-circuiting logical
     * AND of this predicate and the {@code other} predicate
     * @throws NullPointerException if other is null
     */
    default Predicate<T> and(Predicate<? super T> other) {
        Objects.requireNonNull(other);
        return (t) -> test(t) && other.test(t);
    }

    /**
     * Returns a predicate that represents the logical negation of this
     * predicate.
     *
     * @return a predicate that represents the logical negation of this
     * predicate
     */
    default Predicate<T> negate() {
        return (t) -> !test(t);
    }

    /**
     * Returns a composed predicate that represents a short-circuiting logical
     * OR of this predicate and another.  When evaluating the composed
     * predicate, if this predicate is {@code true}, then the {@code other}
     * predicate is not evaluated.
     *
     * <p>Any exceptions thrown during evaluation of either predicate are relayed
     * to the caller; if evaluation of this predicate throws an exception, the
     * {@code other} predicate will not be evaluated.
     *
     * @param other a predicate that will be logically-ORed with this
     *              predicate
     * @return a composed predicate that represents the short-circuiting logical
     * OR of this predicate and the {@code other} predicate
     * @throws NullPointerException if other is null
     */
    default Predicate<T> or(Predicate<? super T> other) {
        Objects.requireNonNull(other);
        return (t) -> test(t) || other.test(t);
    }

    /**
     * Returns a predicate that tests if two arguments are equal according
     * to {@link Objects#equals(Object, Object)}.
     *
     * @param <T> the type of arguments to the predicate
     * @param targetRef the object reference with which to compare for equality,
     *               which may be {@code null}
     * @return a predicate that tests if two arguments are equal according
     * to {@link Objects#equals(Object, Object)}
     */
    static <T> Predicate<T> isEqual(Object targetRef) {
        return (null == targetRef)
                ? Objects::isNull
                : object -> targetRef.equals(object);
    }
}
4.1 test方法
public class PredicateTest {

    public static void main(String[] args) {
        test(msg -> msg.length() > 3,"HelloWorld");
    }

    private static void test(Predicate<String> predicate,String msg){
        boolean b = predicate.test(msg);
        System.out.println("b = " + b);
    }
}
4.2 and、or、negate
public class PredicateTest {

    public static void main(String[] args) {
        test(msg1 -> msg1.contains("H"),msg2->msg2.contains("W"));
    }

    private static void test(Predicate<String> p1,Predicate<String> p2){
        // p1 包含H 同时 p2 包含 W
        boolean b1 = p1.and(p2).test("Hello");
        // p1 包含 H 或者 p2 包含 W
        boolean b2 = p1.or(p2).test("Hello");
        //p1 不包含H
        boolean b3 = p1.negate().test("Hello");

        System.out.println("b1 = " + b1);  //false
        System.out.println("b2 = " + b2); //true
        System.out.println("b3 = " + b3); //false
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值