Lambda表达式----四大核心函数式接口

函数式接口:函数从定义上来说就是某种对应的关系,假设其中的元素为x,对A中的元素x施加对应法则f,记作f(x),得到另一数集B,假设B中的元素为y,则y与x之间的等量关系可以用y=f(x)。函数式接口就是能帮我们完成某种功能的接口。通常用@FunctionalInterface来表明一个接口是函数式接口。java中有很多函数式接口。今天主要学习jdk1.8中内置的四大核心函数式接口。

1.消费型接口,接收一个T类型的参数,无返回

@FunctionalInterface
public interface Consumer<T> {

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

2.供给型接口,获取参数为T类型的值

@FunctionalInterface
public interface Supplier<T> {

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

3.功能型接口,接收一个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);
}

4.断言型接口,判断T类型的参数是否满足条件。

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

下面是对这四种函数式接口的测试:

public class lambda01 {

    //消费型接口测试
    @Test
    public void Test01() {
        consumer(98.5, x -> System.out.println("今天吃了一条鱼,消费了" + x + "元"));
    }
    public void consumer(Double money, Consumer<Double> doubleConsumer) {
        doubleConsumer.accept(money);
    }

    //供给型接口测试
    @Test
    public void Test02() {
        //产生三个10以内的随机数,并打印出来
        List<Integer> list = getList(3, () -> (int) (random() * 10));
        list.forEach(x -> System.out.println(x));
    }
    //获取number个满足条件的数字
    public List<Integer> getList(int number, Supplier<Integer> supplier) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < number; i++) {
            int n = supplier.get();
            list.add(n);
        }
        return list;
    }

    //功能性接口测试
    @Test
    public void Test03() {
        //获取abcdefg字符串的长度
        Integer length = getLength("abcdefg", x -> x.length());
        System.out.println(length);
    }
    //获取字符串的长度
    public Integer getLength(String str, Function<String, Integer> function) {
        return function.apply(str);
    }

    //断言型接口测试
    @Test
    public void Test04() {
        List<Integer> list = Arrays.asList(45, 56, 48, 49, 65, 80);
        //找出大于60的数
        List<Integer> integerList = getList(list, s -> s > 60);
        integerList.forEach(x -> System.out.println(x));
    }
    //将满足条件的加入返回
    public List<Integer> getList(List<Integer> list, Predicate<Integer> predicate) {
        List<Integer> integers = new ArrayList<>();
        for (Integer i : list) {
            if (predicate.test(i)) {
                integers.add(i);
            }
        }
        return integers;
    }

}

打印结果分别为:

今天吃了一条鱼,消费了98.5
5
4
9
7
65
80
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值