Lambda 语法

1.java8 Lambda表达式语法简介 此处需要使用jdk1.8或其以上版本

Lambd表达式分为左右两侧
* 左侧:Lambda 表达式的参数列表
* 右侧:Lambda 表达式中所需要执行的功能, 即Lambda体

Lambda表达式需要“函数式接口”的支持
 函数式接口:接口中只有一个抽象方法的接口,成为函数式接口。 可以使用注解@FunctionalInterface 修饰接口类,因为此注解修饰的接口中只能声明一个抽象方法

2.常见函数式接口对应的Lambda表达式的demo

   /**
     * 语法格式一:无参数,无返回值
     * () -> System.out.println("hello Lambda");
     */
    @Test
    public void test1() {
        int num = 0; //jdk1.7前,必须添加final jdk1.8默认添加了final
        
        Runnable r = new Runnable() {
            
            @Override
            public void run() {
                System.out.println("hello Lambda" + num);
            }
        };
        r.run();
        
        //lambda
        Runnable r1 = () -> System.out.println("hello Lambda" + num);
        r1.run();
    }
    
    /**
     * 语法格式二:有一个参数,无返回值
     * (x) -> System.out.println(x);
     */
    @Test
    public void test2() {
        Consumer<String> con = (x) -> System.out.println(x);
        con.accept("hello test");
    }
    
    /**
     * 语法格式三:只有一个参数,无返回值小括号可以不写
     * x -> System.out.println(x);
     */
    @Test
    public void test3() {
        Consumer<String> con = x -> System.out.println(x);
        con.accept("hello test");
    }
    
    /**
     * 语法格式四:两个参数 ,有返回值,Lambda体中多条语句
     * (x, y) -> {
            System.out.println("函数式接口");
            return Integer.compare(x, y);
        };
     */
    @Test
    public void test4() {
        Comparator<Integer> com = (x, y) -> {
            System.out.println("函数式接口");
            return Integer.compare(x, y);
        };
        System.out.println(com.compare(2, 1));
    }
    
    /**
     * 语法格式五:两个参数 ,有返回值,Lambda体中只有1条语句, 大括号和返回值可以不写
     * (x, y) -> Integer.compare(x, y);
     */
    @Test
    public void test5() {
        Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
        System.out.println(com.compare(2, 1));
    }
    
    /**
     * 语法格式六:Lambda 表达式的参数列表的数据类型可以省略不写,因为jvm编译器通过上下文推断出数据类型,即“类型推断”
     * (Integer x, Integer y) -> Integer.compare(x, y);
     * (x, y) -> Integer.compare(x, y);
     */
    @Test
    public void test6() {
        Comparator<Integer> com = (Integer x, Integer y) -> Integer.compare(x, y);
        System.out.println(com.compare(2, 1));
    }

3.Lambda表达式练习题

练习题1

//对一个数进行运算
@Test
public void test7() { //求10的平方 Integer num = operation(10, (x) -> x*x); System.out.println(num); //求3+2 System.out.println(operation(3, y -> y+2)); } //运算 public Integer operation(Integer number, OperationInterface operationInterface) { return operationInterface.getValue(number); }

@FunctionalInterface
public interface OperationInterface {
    
    Integer getValue(Integer number);
}

 练习题2

List<Employee> emps = Arrays.asList(
            new Employee("张三", 18, 8888.88),
            new Employee("李四", 20, 9999.99),
            new Employee("王五", 22, 5555.55),
            new Employee("赵六", 18, 6666.66)
            );
    
    /*1.调用Collections.sort()方法,通过定制排序比较两个Employee(先按年龄比,年龄相同
      的按姓名比),使用Lambda作为参数传递*/
    @Test
    public void test1(){
        Collections.sort(emps, (x, y) -> {
            if (x.getAge() == y.getAge()) {
                return x.getName().compareTo(y.getName());
            }else{
                return Integer.compare(x.getAge(), y.getAge());
            }
        });
        
        for(Employee emp : emps){
            System.out.println(emp.getName()+","+emp.getAge());
        }
    }
    
    /*2.声明一个带两个翻下的函数式接口,泛型类型为<T, R> T为参数, R为返回值
    接口中声明对应的抽象方法
    在TestLambda2类中声明方法,使用接口作为参数,计算两个long型参数的和
    计算两个long型参数的乘积*/
    @Test
    public void test2(){
        //计算两个数的和
        calculate(10, 10, (x, y) -> x+y);
        
        //计算两个数的乘积
        calculate(10, 10, (x, y) -> x*y);
    }
    
    public void calculate(long t1, long t2, MyFunction<Long, Long> mf){
        long va = mf.getValue(t1, t2);
        System.out.println(va);
    }
@FunctionalInterface
public interface MyFunction<T, R> {
    
    public R getValue(T t1, T t2);
}
原文地址:https://www.cnblogs.com/gyli20170901/p/9792211.html