一文搞懂JAVA函数式编程

一、简介

        jdk1.8引入函数式编程,结合lambda表达式极大提高了开发效率。

二、函数式编程

1.Consumer消费类函数接口

源码

@FunctionalInterface
public interface Consumer<T> {

    //接收一个T类型参数进行处理
    void accept(T t);

    //先执行函数1,再执行函数after
    default Consumer<T> andThen(Consumer<? super T> after) {
        Objects.requireNonNull(after);
        return (T t) -> { accept(t); after.accept(t); };
    }
}

举例:

import java.util.function.Consumer;

public class ConsumerFunctionDemo {
    public static void main(String[] args) {
        //接收s参数 并打印
        Consumer<String> consumer = s -> System.out.println(s + " world");
        consumer.accept("hello");

        System.out.println("---------------------------------------------");

        Consumer<String> consumerAfter = s -> System.out.println(s + " world after");
        //先执行consumer 再执行consumerAfter
        consumer.andThen(consumerAfter).accept("hello");
    }
}

输出结果:

hello world
---------------------------------------------
hello world
hello world after

适用场景:接收一个参数,并对参数进行处理加工。常用方法consumer.accept方法。

2.Supplier 供给类函数接口

源码:

@FunctionalInterface
public interface Supplier<T> {

    //获取结果
    T get();
}

举例:

public class SupplierFunctionDemo {
    public static void main(String[] args) {
        Supplier<List<Integer>> supplier = () -> {
            List<Integer> arr = new ArrayList<>();
            for(int i=0;i<10;i++){
                arr.add(i);
            }
            return arr;
        };

        System.out.println(supplier.get());
    }
}

输出结果:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

应用场景:获取一些加工后的结果

3.Function<T, R>函数式接口

源码:

@FunctionalInterface
public interface Function<T, R> {
    //接受T类型参数处理后返回R类型结果
    R apply(T t);

    //先执行before函数,将before函数结果作为参数传递给当前函数,执行当前函数
    default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
        Objects.requireNonNull(before);
        return (V v) -> apply(before.apply(v));
    }

    //先执行当前函数,将当前函数执行结果作为参数传递给after函数
    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;
    }
}

举例:

public class FunctionDemo {
    public static void main(String[] args) {
        Function<Integer, String> function = x -> "result:"+x;
        String result = function.apply(10);
        System.out.println(result);

        System.out.println("-----------------------------------");
        //先执行function,将function执行结果作为参数传递给functionCom,再执行functionCom
        Function<String, String> functionCom = x -> "com:"+x;
        System.out.println(functionCom.compose(function).apply(10));

        System.out.println("-----------------------------------");
        //先执行当前函数,将当前函数结果作为参数传递给function,执行function函数
        Function<Integer, Integer> functionAfter = x -> x+1;
        System.out.println(functionAfter.andThen(function).apply(10));

        System.out.println("-----------------------------------");
        Function<Integer, Integer> identity = Function.identity();
        System.out.println(identity.apply(10));
    }
}

结果:

result:10
-----------------------------------
com:result:10
-----------------------------------
result:11
-----------------------------------
10

应用场景:接收一个参数处理后返回一个结果,应用比较广泛

4.Predicate 断言形函数

源码:

@FunctionalInterface
public interface Predicate<T> {
    //接受T类型参数,经过逻辑判断后返回boolean类型值
    boolean test(T t);

    //判断当前函数与other函数是否都是逻辑true
    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);
    }

    //当前函数与other函数一个为真返回ture,否则返回false
    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);
    }

}

举例:

public class PredicateFunctionDemo {
    public static void main(String[] args) {
        Predicate<Integer> predicate = x -> x>10;
        System.out.println(predicate.test(100));

        System.out.println("--------------------------------");
        Predicate<Integer> predicateOther = x -> x<100;
        System.out.println(predicate.and(predicateOther).test(100));

        System.out.println("--------------------------------");
        System.out.println(predicate.negate().test(100));

        System.out.println("--------------------------------");
        System.out.println(predicate.or(predicateOther).test(100));
        System.out.println("--------------------------------");
        Predicate<Integer> function2 = Predicate.isEqual(10);
        System.out.println(function2.test(10));
    }
}

结果:

true
--------------------------------
false
--------------------------------
false
--------------------------------
true
--------------------------------
true

应用场景:逻辑结果判断

5.自定义函数

自定义函数是一个接口类,需要加上@FunctionalInterface注解标记是一个函数接口

@FunctionalInterface
public interface MyFunction<T> {
    //传入两个整数 返回一个整数
    Integer apply(T a, T b);
}

举例:

public class MyFunctionDemo {
    public static void main(String[] args) {
        //获取最大值
        MyFunction<Integer> myFunction = (x, y) -> {
            return x>y?x:y;
        };
        System.out.println(myFunction.apply(6, 10));
    }
}

结果:

10

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值