Java8新特性之Lambda表达式

1-Lambda表达式

Lambda是一个匿名函数,我们可以把Lambda表达式理解为一段可以传递的代码(将代码像数据一样进行传递)。可以写出更简洁、更灵活的代码。

//原来的匿名内部类
    @Test
    public void test1(){
        Comparator<Integer> comparator = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return Integer.compare(o1,o2);
            }
        };
        TreeSet<Integer> ts = new TreeSet<>(comparator);
    }

    //Lambda表达式
    @Test
    public void test2(){
        Comparator<Integer> comparator = (x,y)->Integer.compare(x,y);
        TreeSet<Integer> ts = new TreeSet<>(comparator);
    }

 Lambda表达式在Java语言中引入了"->"操作符,该操作符被称为Lambda操作符。它将Lambda分为两部分:

左侧:指定了Lambda表达式所需要的所有参数

右侧:指定了Lambda体,即Lambda表达式要执行的功能

Lambda表达式语法

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

Runnable run = ()->System.out.println("Hello Lambda!");

语法二:Lambda需要一个参数

Consumer<String> fun = (args)-> System.out.println(args);

语法三:Lambda需要一个参数是,参数的小括号可以省略

Consumer<String> fun = args-> System.out.println(args);

语法四:Lambda需要两个参数,并且有返回值

BinaryOperator<Long> bo = (x,y)->{
    System.out.println("实现函数接口方法");
    return x+y;
}

语法五:当Lambda只有一条语句时,return与大括号可以不写

BinaryOperator<Long> bo = (x,y)-> return x+y;

 语法六:数据类型可以省略,因为可以由编译器推断得出,称为“类型推断” ,这是因为javac根据程序的上下文,在后台推断出参数的类型。

BinaryOperator<Long> bo = (Long x,Long y)->{
    System.out.println("实现函数接口方法");
    return x+y;
}

 函数式接口

  1. 只包含一个抽象方法的接口,称为函数式接口
  2. 你可通过Lambda表达式来创建该接口的对象。
  3. 我们可以在任意函数式接口上使用@FunctionalInterface注解,这样做可以检查它是否是一个函数式接口,同时,javadoc也会包含一条声明,说明这个接口是一个函数式接口
@FunctionalInterface
public interface MyFun<T> {
    public T getValue(T num);
}
public String toUpperString(MyFun<String> mf,String str){
    return mf.getValue(str);
}
String newStr = toUpperString(str->str.toUpperCase(),"abcdef");

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

 方法引用

若Lambda体中的内容有已经实现的方法,我们可以使用”方法引用“(可以理解为方法引用是Lambda表达式的另外一种表现形式)

主要的格式有三种:

  1. 对象::实例方法名
  2. 类::静态方法名
  3. 类::实例方法名

Lambda体中调用方法的参数列表和返回值类型,要与函数式接口中抽象方法的函数列表和返回类型保持一致

若Lambda参数列表中第一个参数是实例方法的调用者,而第二个参数是实例方法的参数时,可以使用ClassName::method

构造器引用

格式:

ClassName::new

数组引用

格式

Type[]::new

    //对象::实例方法
    @Test
    public void test1(){
        Consumer<String> con = (x)-> System.out.println(x);

        PrintStream ps = System.out;
        Consumer<String> consumer = ps::println;
    }

    @Test
    public void test2(){
        Employee employee = new Employee();
        Supplier<String> sup = ()->employee.getName();
        String str = sup.get();
        System.out.println(str);

        Supplier<Integer> sup1 = employee::getAge;
        Integer integer = sup1.get();
        System.out.println(integer);
    }


    //类::静态方法名
    public void test3(){
        Comparator<Integer> com = (x,y)->Integer.compare(x,y);

        Comparator<Integer> com2 = Integer::compare;
    }

    //类::实例方法
    @Test
    public void test4(){
        BiPredicate<String,String> bp = (x,y)->x.equals(y);
        BiPredicate<String,String> bp2 = String::equals;
    }

    //构造器引用
    @Test
    public void test5(){
        Supplier<Employee> supplier = ()->new Employee();
        supplier.get();

        Supplier<Employee> supplier2 = Employee::new;//无参构造器
        Employee emp = supplier2.get();
        System.out.println(emp);

    }

    @Test
    public void test6(){
        Function<Integer,Employee> fun = (x)->new Employee(x);
        Function<Integer,Employee> fun2 = Employee::new;
        Employee employee = fun2.apply(35);
        System.out.println(employee);
    }

    //数组引用:
    @Test
    public void test7(){
        Function<Integer,String[]> fun = (x)->new String[x];
        String[] apply = fun.apply(10);
        System.out.println(apply.length);

        Function<Integer,String[]> fun2 = String[]::new;
        String[] apply1 = fun2.apply(20);
        System.out.println(apply1.length);
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值