四大函数式接口演示代码——>通俗易懂

四大函数式接口演示代码——>通俗易懂

函数式接口

​ 函数式接口是指除Object中的方法之外只有一个抽象方法的接口。函数式接口无疑是为使得程序变得特别简单而出现的,原来几行的代码,现在可能使用一个lanmada表达式一行就可以了,本文列举了几个常见的函数式接口供大家参考。

一:Function

首先定义一个Student供给测试使用

public class Student {
    private String name;

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                '}';
    }

    public Student(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

用法:提供一个T类对象,返回 R类对象

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

演示代码

/**
 * @Author: 何十一
 * @Date: Created in 2021/3/7
 * @Description:函数式接口1 --->   提供一个T类对象,返回 R类对象
 */
public class test1 {
    public static void main(String[] args) {
        //新建一个对象
        Student student = new Student("何十一");
        //方式一
        Function function1 = new Function<Student, String>() {
            @Override
            public String apply(Student student) {
                return student.getName();
            }
        };

        //方式二
        Function<Student, String> function2 = (Student stu) -> {
            return stu.getName();
        };

        //方式三
        Function<Student, String> function3 = (Student stu) -> stu.getName();

        System.out.println(function1.apply(student));
        System.out.println(function2.apply(student));
        System.out.println(function3.apply(student));
    }
}

二:Predicate

用法:提供一个T类对象,返回boolean类型

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

演示代码

/**
 * @Author: 何十一
 * @Date: Created in 2021/3/7
 * @Description:函数式接口2  --->   提供一个T类对象,返回boolean类型
 */
//public interface Predicate<T> {
//    boolean test(T t);
public class test2 {
    public static void main(String[] args) {
        //方式一
        Predicate predicate1=new Predicate<String>() {
            @Override
            public boolean test(String str) {
                return str.isEmpty();
            }
        };
        //方式二
        Predicate<String> predicate2=(str)->{return str.isEmpty();};

        //方式三
        Predicate<String> predicate3=(str)-> str.isEmpty();

        System.out.println(predicate1.test("何十一"));
        System.out.println(predicate2.test("何十一"));
        System.out.println(predicate3.test("何十一"));
    }
}

三:Consumer

用法:提供一个T类对象,重写方法进行消费,无返回值

@FunctionalInterface
public interface Consumer<T> {

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

演示代码

/**
 * @Author: 何十一
 * @Date: Created in 2021/3/7
 * @Description:函数式接口3  --->   提供一个T类对象,重写方法进行消费,无返回值
 */
//public interface Consumer<T> {
//    void accept(T t);
public class test3 {
    public static void main(String[] args) {
        Student student = new Student("何十一");
        //方式一
        Consumer<Student> consumer1 = new Consumer<Student>() {
            @Override
            public void accept(Student stu) {
                System.out.println(stu);
            }
        };
        //方式二
        Consumer<Student> consumer2 = (Student stu) -> {
            System.out.println(stu);
        };

        consumer1.accept(student);
        consumer2.accept(student);
    }
}

四:Supplier

用法:不提供对象,生产一个T类对象返回

@FunctionalInterface
public interface Supplier<T> {

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

演示代码

/**
 * @Author: 何十一
 * @Date: Created in 2021/3/7
 * @Description:函数式接口1 --->   不提供对象,生产一个T类对象返回
 */
//public interface Supplier<T> {
//    T get();
public class test4 {
    public static void main(String[] args) {
        //方式一
        Supplier<Student> supplier1 = new Supplier<Student>() {
            @Override
            public Student get() {
                return new Student("何十一");
            }
        };

        //方式二
        Supplier<Student> supplier2 = () -> {
            return new Student("何十一");
        };

        //方式三
        Supplier<Student> supplier3 = () -> new Student("何十一");

        System.out.println(supplier1.get());
        System.out.println(supplier2.get());
        System.out.println(supplier3.get());
    }
}

五:自定义函数式接口,MyFunction

用法:自定义函数式接口,传递T,R,返回E

//添加FunctionalInterface注解,检测该接口是不是函数式接口(除Object中的方法之外只有一个抽象方法的接口,本例去掉无影响)
@FunctionalInterface  
public interface MyFunction<T,R,E> {
    E myGet(T t, R r);
}

演示代码

/**
 * @Author: 何十一
 * @Date: Created in 2021/3/7
 * @Description:自定义函数式接口,传递T,R,返回E
 */
public class test5 {
    public static void main(String[] args) {
        //方式一
        MyFunction<Student,Integer,String> myFunction1=new MyFunction<Student, Integer, String>() {
            @Override
            public String myGet(Student student, Integer integer) {
                return student.getName()+integer;
            }
        };
        //方式二
        MyFunction<Student,Integer,String> myFunction2= (student, integer) -> student.getName()+integer;
        Student student = new Student("何十一");

        System.out.println(myFunction1.myGet(student, 1));
        System.out.println(myFunction2.myGet(student, 6));
    }
}

希望对你有帮助!

近期文章
单例模式和双重检测锁模式下的相关问题
Springboot-Aop基于正则表达式和注解实现
SpringBoot整合Redis及简单使用
Docker安装Mysql以及Mysql的基本操作——入门必看
vue-cli十分钟学习入门笔记――开袋即食
如何判断2的n次方?用四种方式来扒一扒。
关于SpringAOP的三种实现方式你有了解过吗?
八皇后问题详细另类图解-九张图带你了解什么是八皇后问题
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值