Java 函数式接口(Function / Predicate / Supplier / Consumer)

函数式接口

接口中只有一个抽象方法,称为函数式接口
可以包含其他类型方法

判断方法:
在接口前加 @FunctionalInterface如果不是函数式接口会报错

@FunctionalInterface
public interface MyFunctionalInterface {
    public abstract void method();
    //public abstract void method2();
}

使用方法

作为方法参数返回值
1. 在方法中把接口当作参数传入,在方法中可以使用接口变量
2. 当使用该方法时,需要对接口进行实现

public class Test {
    //定义一个方法,参数使用函数式接口MyFunctionalInterface
    public static void show(MyFunctionalInterface mfi){
        mfi.method();
    }

    public static void main(String[] args) {
        //调用show方法,方法的参数是一个接口,所以可以传递接口的实现类对象
        show(new MyFunctionalInterfaceImpl());

        //也可以传递接口的匿名内部类,对接口方法进行实现(有几个方法实现几个方法)
        show(new MyFunctionalInterface() {
            @Override
            public void method() {
                System.out.println("内部类实现接口");
            }
        });

        //调用show方法,方法参数是一个函数式接口,所以可以用lambda
        show(()->{
            System.out.println("lambda实现函数式接口");
        });

        //lambda简化书写
        show(()-> System.out.println("lambda简化实现函数式接口"));
    }
}

常用函数式接口

Function

两个泛型 apply根据根据一个类型的数据得到另一个类型的数据。

@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);
}
public class FunctionTest {
    public static void main(String[] args) {
        Function<Integer,Integer> function = new Function<Integer, Integer>() {
            @Override
            public Integer apply(Integer integer) {
                return integer + 10;
            }
        };
        System.out.println(function.apply(10));
        
		//简化
		Function<Integer,Integer> function = (x)->{return x+10;};
    }
}

Predicate

test用来对指定数据类型进行判断,符合条件返回true不符合返回false

@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);
}

测试

@Test
public void testPredicate(){
    Predicate<Integer> predicate = x->{
        return x > 10;
    };

    System.out.println(predicate.test(20));
}

其中还有多重判断方法and多个test同时满足,negatetest相反判定,or满足一个test判定即可。

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);
}

通过链式编程,三个多重判断方法返回的还是Predicate对象本身,即可以继续调用

@Test
public void testPredicate(){
    Predicate<Integer> predicate = x-> x > 10;
    Predicate<Integer> predicate2 = x-> x < 20;
    
    System.out.println(predicate.and(predicate2).test(15));
}

Supplier

Supplier<T> 接口被称为生产型接口,输入什么类型get就会返回什么类型

@FunctionalInterface
public interface Supplier<T> {

    /**
     * Gets a result.
     *
     * @return a result
     */
    T get();
}
@Test
public void testSupplier(){
    Supplier<String> supplier = ()->"cccrj";

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

Consumer

Consumer<T>是一个消费型接口,泛型执行什么类型,就可以用accept消费什么类型数据

@FunctionalInterface
public interface Consumer<T> {

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

andThen()两个消费者同时消费输入值

@Test
public void testConsumer(){
    Consumer<Integer> consumer = System.out::println;
    int count = 2;
    consumer.accept(count);
    System.out.println("------------");
    consumer.andThen(consumer).accept(count);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值