lambda表达式基础语法及其使用

1、无参、无返回值

    @Test
    public void test00() {
        Runnable r1 = new Runnable() {
            @Override
            public void run() {
                System.out.println("Lambda01.test01().new Runnable() {...}.run()");
            }
        };
        r1.run();
    }

2、无参、无返回值

    @Test
    public void test01() {
        Runnable r2 = () -> System.out.println("hello lambda !");
        r2.run();
    }

3、有一个参数、且无返回值(有一个参数小括号可以省略不写)

    @Test
    public void test02() {
        Consumer<String> con = (x) -> System.out.println(x);
        // Consumer<String> con = x -> System.out.println(x);
        con.accept("this is consumer!");
    }

4、如果有2个参数、有返回值,函数体中有多条语句要用{}和return

    @Test
    public void test03() {
        Comparator<Integer> com = (x, y) -> {
            System.out.println("函数式接口");
            return Integer.compare(x, y);
        };
        System.out.println(com.compare(3, 2));
    }

5、如果有2个参数、有返回值,函数体中只有一条可以省略{}和return

    @Test
    public void test04() {
        Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
        System.out.println(com.compare(3, 2));
    }

6、需求:对一个数进行运算

    @Test
    public void test05() {
        System.out.println(operate(100, (x) -> x + x));
        System.out.println("-------------------");
        System.out.println(operate(100, (x) -> x * x));
    }

    private Integer operate(Integer num, LambdaFun01 lf) {
        return lf.getValue(num);
    }
package com.zh.lambda;

@FunctionalInterface
public interface LambdaFun01 {

    Integer getValue(Integer t);

}

(1)lambda表达式需要函数式接口的支持

(2)函数式接口:接口中只有一个抽象方法的接口,称为函数式接口。可以使用@FunctionalInterface修饰

 1    调用Collertions.sort()方法,通过定制排序比较两个Employee(先按年龄比,年龄相同按姓名比),使用Lambda作为参数传递。
 2    ①声明函数式接口,接口中声明抽象方法,public string getValue(string str);
       ②声明类Testlambda,类中编写方法使用接口作为参数,将一个字符串转换成大写,并作为方法的返回值。
       ③再将一个字符串的第2个和第4个索引位置进行截取子串。
 3    ①声明一个带两个泛型的函数式接口,泛型类型为<T,R>T为参数,R为返回值
       ②接口中声明对应抽象方法。
       ③在Testlambda类中声明方法,使用接口作为参数,计算两个long型参数的和。
       ④再计算两个long型参数的乘积。

    List<Employee> emps = Arrays.asList(//
            new Employee(20, "张三", 5000.35), //
            new Employee(40, "李四", 6500.63), //
            new Employee(30, "王五", 4000.93), //
            new Employee(50, "赵六", 9005.36), //
            new Employee(10, "马七", 1050.93), //
            new Employee(20, "朱八", 3000.73)//
    );

    // 1、无参、无返回值
    @Test
    public void test01() {
        Collections.sort(emps, (e1, e2) -> {
            if (e1.getAge() == e2.getAge()) {
                return e1.getName().compareTo(e2.getName());
            } else {
                return e1.getAge().compareTo(e2.getAge());
            }
        });
        for (Employee employee : emps) {
            System.out.println(employee);
        }
    }

    @Test
    public void test02() {
        String str1 = strHandler("asdfasdfasdf", x -> x.toUpperCase());
        System.out.println(str1);

        String str2 = strHandler("asdfasdfasdf", x -> x.substring(3, 5));
        System.out.println(str2);
    }

    private String strHandler(String str, LambdaFun02 lFun02) {
        return lFun02.getValue(str);
    }

    @Test
    public void test03() {
        calc(10l, 20l, (l1, l2) -> l1 + l2);
        calc(10l, 20l, (l1, l2) -> l1 * l2);
    }

    private void calc(Long l1, Long l2, LambdaFun03<Long, Long> lambdaFun03) {
        System.out.println(lambdaFun03.getValue(l1, l2));
    }

7、函数LambdaFun02

package com.zh.lambda;

@FunctionalInterface
public interface LambdaFun02 {

    String getValue(String t);

}

8、函数LambdaFun03

package com.zh.lambda;

@FunctionalInterface
public interface LambdaFun03<R, T> {

    R getValue(T t1, T t2);

}

 

posted on 2019-11-12 22:11  牛鼻子老赵  阅读(1830)  评论(0编辑  收藏  举报