Lambda表达式

Lambda表达式

Ladmaba简介

Lambda表达式可以理解成可传递的匿名函数的一种方式,由参数、箭头和主体组成

Lambada表达式只能配合函数式接口使用,那么什么是函数式接口呢?

  • 函数式接口:只有一个抽象方法的接口,可以用@FunctionalInterface标记

java8设计的几个常见的函数式接口

  1. Predicate
@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);
    ...
}
  1. Consumer
@FunctionalInterface
public interface Consumer<T> {

    /**
     * Performs this operation on the given argument.
     *
     * @param t the input argument
     */
    void accept(T t);
    ...
}
  1. Function
@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);
    ...
}
注意

任何函数式接口都不能抛出异常
那么如果我们需要Lambda表达式抛出异常,怎么办呢?这里提供两种解决方案

  1. 定义及自己的函数接口
package io.github.jiangdequan;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.InputStreamReader;

public class ReadProcessor {
    public static void main(String[] args) throws Exception {
        File file = new File("D:\\missj\\java8_actual_combat\\Lambda表达式\\README.md");
        
        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
        FileProcessor processor = (arg) -> arg.readLine();
        String content = processor.process(bufferedReader);
        System.out.println(content);
    }
    
}

@FunctionalInterface
public interface FileProcessor{
    String process(BufferedReader bufferedReader) throws Exception;
}

  1. 使用java8的函数式接口显示的捕捉受检异常
package io.github.jiangdequan;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.function.Function;

public class ReadProcessor {
    public static void main(String[] args) throws Exception {
        File file = new File("D:\\missj\\java8_actual_combat\\Lambda表达式\\README.md");
        
        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
        Function<BufferedReader, String> processor = (BufferedReader b) -> {
            b.readLine();
        };

        String content = processor.apply(bufferedReader);
        System.out.println(content);
    }
    
}

实战

我们将苹果按照重量排序

import java.util.ArrayList;
import java.util.List;

import static java.util.Comparator.comparing;

public class Filter {

    public static void main(String[] args) {
        Apple apple = new Apple(2.3, 5.0, "green");
        Apple apple1 = new Apple(1.0, 15.0, "red");
        Apple apple2 = new Apple(2.0, 10.0, "red");
        List<Apple> list = new ArrayList<>();
        list.add(apple);
        list.add(apple1);
        list.add(apple2);

        list.sort(comparing(Apple::getColor));
        list.forEach(arg -> System.out.println(arg.toString()));
    }
    
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值