java8新特性(一):Lambda表达式

目录

 

为什么使用Lambda 表达式

Lambda 表达式语法

类型推断

函数式接口

方法引用与构造器引用


为什么使用Lambda 表达式

Lambda 是一个匿名函数,我们可以把Lambda表达式理解为是一段可以传递的代码(将代码像数据一样进行传递)。可以写出更简洁、更灵活的代码。作为一种更紧凑的代码风格,使Java的语言表达能力得到了提升。

从匿名类到 Lambda 的转换:

        //匿名内部类
        Comparator<Integer> com = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return Integer.compare(o1,o2);
            }
        };
//Lambda表达式
Comparator<Integer> com = (x,y)->Integer.compare(x,y);
或者
Comparator<Integer> com = Integer::compare;

Lambda 表达式语法

Lambda 表达式在Java 语言中引入了一个新的语法元素和操作符。这个操作符为 “->” , 该操作符被称为 Lambda 操作符或剪头操作符。它将 Lambda 分为两个部分:
左侧:指定了 Lambda 表达式需要的所有参数
右侧:指定了 Lambda 体,即 Lambda 表达式要执行的功能。

语法格式一:无参,无返回值,Lambda体只需要一条语句

    /**
     * @Description: 语法格式一:无参,无返回值,Lambda体只需要一条语句
     * @date: 2018/8/8
     * @param: []
     * @return: void
     */
    @Test
    public void test1() {
        int num = 10;//jdk1.7前,必须加final,jdk1.8会自动加上
        Runnable runnable = () -> System.out.println("Hello World!" + num);
        runnable.run();
    }
语法格式二:Lamdab需要一个参数,并且无返回值
    /***
     * @Description: 语法格式二:Lamdab需要一个参数,并且无返回值
     * @date: 2018/8/8
     * @param: []
     * @return: void
     */
    @Test
    public void test2() {
        Consumer<String> com = (x) -> System.out.println(x);
        com.accept("Hello World!");
    }
语法格式三:Lambda只需要一个参数时.参数的小括号可以省略
    /**
     * @Description: 语法格式三:Lambda只需要一个参数时.参数的小括号可以省略
     * @date: 2018/8/8
     * @param: []
     * @return: void
     */
    @Test
    public void tess3() {
        Consumer<String> com = x -> System.out.println(x);
        com.accept("Hello World!");

    }
语法格式四:Lambda需要两个参数.并且有返回值,多条语句时必须加上{}
    /**
     * @Description: 语法格式四:Lambda需要两个参数.并且有返回值,多条语句时必须加上{}
     * @date: 2018/8/8
     * @param:
     * @return:
     */
    @Test
    public void test4() {
        BinaryOperator<Integer> bi = (x, y) -> {
            System.out.println("加法");
            return x + y;
        };
        Integer apply = bi.apply(23, 45);
        System.out.println(apply);
    }
语法格式五:当lambda体只有一条语句时,return与大括号可以省略
    /**
     * @Description: 语法格式五:当lambda体只有一条语句时,return与大括号可以省略
     * @date: 2018/8/8
     * @param:
     * @return:
     */
    @Test
    public void test5() {
        BinaryOperator<Integer> bi = (x, y) -> x + y;
        System.out.println(bi.apply(20, 30));
    }
语法格式六:Lambda的参数列表的类型可以不写,因为JVM编译器可以通过上下文进行"类型推断".
    /**
     * @Description: 语法格式六:Lambda的参数列表的类型可以不写,因为JVM编译器可以通过上下文进行"类型推断".
     * @date: 2018/8/8
     * @param:
     * @return:
     */
    @Test
    public void test6() {
        //BinaryOperator<Integer> bi = (Integer x,Integer y)-> x+y;
        BinaryOperator<Integer> bi = (x, y) -> x + y;
        System.out.println(bi.apply(20, 30));
    }

类型推断

上述 Lambda 表达式中的参数类型都是由编译器推断得出的。Lambda 表达式中无需指定类型,程序依然可以编译,这是因为 javac 根据程序的上下文,在后台推断出了参数的类型。Lambda 表达式的类型依赖于上下文环境,是由编译器推断出来的。这就是所谓的 “类型推断”.

函数式接口

只包含一个抽象方法的接口,称为函数式接口。
你可以通过 Lambda 表达式来创建该接口的对象。(若 Lambda表达式抛出一个受检异常,那么该异常需要在目标接口的抽象方法上进行声明)。
我们可以在任意函数式接口上使用 @FunctionalInterface 注解,这样做可以检查它是否是一个函数式接口,同时 javadoc 也会包含一条声明,说明这个接口是一个函数式接口。

自定义函数式接口

@FunctionalInterface
public interface MyFunction {
     String getValue(String str) ;
}
@FunctionalInterface
public interface Myfunction2<T, R> {
    R getValue(T t1, T t2);
}

作为参数传递Lambda 表达式 

    // 数据处理方法
    public String strHandler(String str, MyFunction function) {
        return function.getValue(str);
    }

    //自定义函数式接口, 并使用Lambda表达式
    @Test
    public void test2() {
        String result = strHandler("adbcdswe", x -> x.toUpperCase());
        System.out.println(result);

    }

    public void op(Integer num1, Integer num2, Myfunction2<Integer, Integer> my) {
        System.out.println(my.getValue(num1, num2));
    }

    public void test3() {
        op(20, 30, (x, y) -> x + y);
    }

 注意:作为参数传递 Lambda 表达式:为了将 Lambda 表达式作为参数传递,接收Lambda 表达式的参数类型必须是与该 Lambda 表达式兼容的函数式接口的类型。

Java 8内置四大核心函数式接口

函数式接口 参数类型 返回类型 用途
Consumer<T>消费型接口 T void 对类型为T的对象应用操作,包含方法:void accept(T t)
Supplier<T>供给型接口 T 返回类型为T的对象,包含方法:T get();
Function<T, R>函数型接口 T R 对类型为T的对象应用操作,并返回结果。结果是R类型的对象。包含方法:R apply(T t);
Predicate<T>断定型接口 T boolean 确定类型为T的对象是否满足某约束,并返回boolean 值。包含方法boolean test(T t);
    //Consumer<T>
    @Test
    public void test1() {
        Map<String, Double> map = new HashMap<>();
        map.put("money", 100000d);
        consumer(map, x -> x.put("money", x.get("money") - 2000d));
        System.out.println(map);
    }


    public void consumer(Map<String, Double> money, Consumer<Map<String, Double>> con) {
        con.accept(money);
    }
    //供给型接口
    @Test
    public void test2() {
        List<Integer> list = getNumList(3, () -> {
            Random random = new Random();
            int i = random.nextInt(100);
            System.out.println("产生整数:" + i);
            return i;
        });
        System.out.println(list);
    }
    //生成指定个数的整数
    public List<Integer> getNumList(int num, Supplier<Integer> sup) {
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            list.add(sup.get());
        }
        return list;
    }
    //Function<T,R> : 函数型接口
    @Test
    public void test3() {
        String name = "Hello";
        String converString = converString(name, (x) -> x + UUID.randomUUID().toString().replace("-", ""));
        System.out.println(converString);
    }
    //对字符串处理
    public String converString(String str, Function<String, String> fun) {
        return fun.apply(str);
    }

    //Predicate<T> : 断定型接口
    @Test
    public void test4() {
        List<String> stringList = Arrays.asList("sfasf", "iir", "jjdfsj", "sf");
        List<String> result = converList(stringList, (x) -> x.length() >= 5);
        System.out.println(result);
    }

    //将满足条件的字符串放入新集合中
    public List<String> converList(List<String> list, Predicate<String> pre) {
        List<String> result = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if (pre.test(list.get(i))) {
                result.add(list.get(i));
            }
        }
        return result;
    }

其他接口

 方法引用与构造器引用

当要传递给Lambda体的操作,已经有实现的方法了,可以使用方法引用!(实现抽象方法的参数列表,必须与方法引用方法的参数列表保持一致!)方法引用:使用操作符“::” 将方法名和对象或类的名字分隔开来。
如下三种主要使用情况:
对象::实例方法
类::静态方法
类::实例方法

/**
 * @Description:
 * 一·方法引用 :
 * 当要传递给Lambda体的操作,已经有实现的方法了,可以使用方法引用!
 * 方法引用:使用操作符 “ ::” 将方法名和对象或类的名字分隔开来。

 *  三种情况:对象::实例方法
            类::静态方法
            类::实例方法
 * 注意:
 * ①Lambda体中调用方法的参数列表或返回值类型,必须与函数式接口中抽象方法的函数列表或返回值类型保持一致!
 * ②当需要引用方法的第一个参数是调用对象,并且第二个参数是需要引用方法的第二个参数(或无参数)时: ClassName::methodName (例如:test4)
 *
 *
 * 二·构造器引用:
 *       格式: ClassName::new
 *       注意:  构造器参数列表要与接口中抽象方法的参数列表一致!
 * 三·数组引用
 *       格式: type[]::new
 * @author: Bryce
 * @date: 2018/8/10
 * @version: 1.0
 */
public class TestMethodRef {
    /**
     * @Description: 对象::实例方法
     * @date: 2018/8/10
     * @param:
     * @return:
     */
    @Test
    public void test1 (){
        Consumer<String> con = (x)-> System.out.println(x);
        //PrintStream ps = ;
        //Consumer<String> conRef = ps::println;
        Consumer<String> conRef = System.out::println;
        conRef.accept("helloworld");
        //(x)-> System.out.println(x);等同于System.out::println;
    }
    /**
     * @Description: 对象::实例方法
     * @date: 2018/8/10
     * @param:
     * @return:
     */
    @Test
    public void test2 (){
        Employee em = new Employee("lisi", 22);
        Supplier<String> su = () -> em.getName();
        String str = su.get();
        System.out.println(str);
        Supplier<String> suRef = em::getName;
        System.out.println(suRef.get());

        //() -> em.getName();简化为em::getName;
    }
    /**
     * @Description: 类::静态方法
     * @date: 2018/8/10
     * @param:
     * @return:
     */
    @Test
    public void test3 (){
        Comparator<Integer> com = (x,y) -> Integer.compare(x, y);
        Comparator<Integer> comRef = Integer::compare;
        //Integer::compare;等同于(x,y) -> Integer.compare(x, y);
    }
    /**
     * @Description: 类::实例方法
     * @date: 2018/8/10
     * @param:
     * @return:
     */
    @Test
    public void test4 (){
        //当需要引用方法的第一个参数是调用对象,并且第二个参数是需要引
        //用方法的第二个参数(或无参数)时,才能使用
        BiPredicate<String,String> bi = (x,y) -> x.equals(y);
        BiPredicate<String,String> biRef = String::equals;
        //String::equals;等同于(x,y) -> x.equals(y);

        Function<Employee,String> fun = (e)->e.getName();
        Function<Employee,String> funRef = Employee::getName;

    }
    /**
     * @Description: 构造器引用
     * @date: 2018/8/10
     * @param:
     * @return:
     */
    @Test
    public void test5 (){
        Supplier<Employee> sup = () -> new Employee();
        Supplier<Employee> supRef = Employee::new;
        System.out.println(supRef.get());
        Function<Integer,Employee> fun = (x) -> new Employee(x);
        Function<Integer, Employee> funRef = Employee::new;
        System.out.println(funRef.apply(20));
    }
    /**
     * @Description: 数组引用
     * @date: 2018/8/10
     * @param:
     * @return:
     */
    @Test
    public void test6 (){
        Function<Integer,String[]> fun = (x) -> new String[x];

        Function<Integer,String[]> funRef = String[]::new;

        String[] strings = funRef.apply(10);
        System.out.println(strings.length);

    }
}

java8新特性(二):StreamAPI

猜你喜欢

转载自blog.csdn.net/Duingold/article/details/81748513