Java基础(五):函数式接口

被问到:什么是函数式接口?分为几类?

一脸懵逼,没听过这个概念。回来补一下。

概念:

函数式接口(Functional Interface)就是一个有且仅有一个抽象方法,但是可以有多个非抽象方法的接口。可包含Object的public方法。

使用:

  • 函数式接口可以被隐式转换为 lambda 表达式。
  • 可用@FunctionalInterface 来表名是函数式接口,可用来编写时的错误检查。

示例:

interface FunctionInterface {
    public void say(String message);
}

public class FunctionTest {
    public static void main(String[] args) {
        //使用Lambda表达式来表示该接口的一个实现(之前使用匿名内部类)
        FunctionInterface functionInterface = message -> System.out.println(message);
        functionInterface.say("hello");
    }
}

//输出结果:
hello

 

 

四大分类:供给型接口、消费型接口、函数型接口、断言型接口

 

供给型接口:用来获取一个泛型参数指定类型的对象数据。

示例:

    //可以用来生成一定规则的对象集合    
    @Test
    public void test03() {
        List<Integer> list = getList(10, () -> (int) (Math.random() * 100));
        for (Integer integer : list) {
            System.out.println(integer);
        }
    }

    public List<Integer> getList(int size, Supplier<Integer> supplier) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            Integer num = supplier.get();
            list.add(num);
        }
        return list;
    }

 消费型接口:接口则正好与Supplier接口相反,它不是生产一个数据,而是消费一个数据

示例:

    class User {
        private String name;

        private int age;

        public User(String name, int age) {
            this.name = name;
            this.age = age;
        }

       //此处省略get/set方法
        @Override
        public String toString() {
            return this.getName() + "----" + this.getAge();
        }
    }

    @Test
    public void test02() {
        List<User> userList = new ArrayList<>();
        User user1 = new User("张三", 20);
        User user2 = new User("李四", 40);
        userList.add(user1);
        userList.add(user2);
        userList.stream().forEach(p -> System.out.println(p));
    }

//输出:
张三----20
李四----40

函数型接口 :接口用来根据一个类型的数据得到另一个类型的数据

    @Test
    public void test04() {
        List<User> userList = new ArrayList<>();
        User user1 = new User("张三", 20);
        User user2 = new User("李四", 40);
        userList.add(user1);
        userList.add(user2);
        List<String> nameList = userList.stream().map(User::getName).collect(Collectors.toList());
        nameList.stream().forEach(p -> System.out.println(p));
    }

//输出:
张三
李四

断言型接口 :需要对某种类型的数据进行判断,得到一个boolean值结果

public class PredicateTest {
    public static void main(String args[]){
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);

        // Predicate<Integer> predicate = n -> true
        // n 是一个参数传递到 Predicate 接口的 test 方法
        // n 如果存在则 test 方法返回 true

        System.out.println("输出所有数据:");

        // 传递参数 n
        eval(list, n->true);

        // Predicate<Integer> predicate1 = n -> n%2 == 0
        // n 是一个参数传递到 Predicate 接口的 test 方法
        // 如果 n%2 为 0 test 方法返回 true

        System.out.println("输出所有偶数:");
        eval(list, n-> n%2 == 0 );

        // Predicate<Integer> predicate2 = n -> n > 3
        // n 是一个参数传递到 Predicate 接口的 test 方法
        // 如果 n 大于 3 test 方法返回 true

        System.out.println("输出大于 3 的所有数字:");
        eval(list, n-> n > 3 );
    }

    public static void eval(List<Integer> list, Predicate<Integer> predicate) {
        for(Integer n: list) {

            if(predicate.test(n)) {
                System.out.println(n + " ");
            }
        }
    }
}

//输出结果:
输出所有数据:
1 
2 
3 
4 
5 
6 
7 
8 
9 
输出所有偶数:
2 
4 
6 
8 
输出大于 3 的所有数字:
4 
5 
6 
7 
8 
9 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值