Java8新特性--Lambda表达式

文章介绍了如何在Java中使用Lambda表达式简化代码,特别是对于List的定制排序。通过示例展示了Lambda如何替代Comparator的匿名实现类,以及Lambda的基本语法和针对四大函数式接口(Consumer、Supplier、Function、Predicate)的使用。
摘要由CSDN通过智能技术生成

Lambda表达式

使用背景

简化代码开发

示例

实现List的定制排序
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Student {
    private Long id;
    private String name;
    private Integer age;
}
public class StudentUtil {
    private static List<Student> studentList;
    static {
        studentList = new ArrayList<Student>();
        Student student1 = new Student(1L,"www",19);
        Student student2 = new Student(2L,"ddd",18);
        Student student3 = new Student(3L,"lll",20);
        studentList.add(student1);
        studentList.add(student2);
        studentList.add(student3);
    }
    public static List<Student> getStudentList(){
        return studentList;
    }
}
当前有一个学生集合,现需要对学生按照年龄升序排列,调用List接口的sort方法,需要传入定制排序Comparator的匿名实现类,代码如下:
List<Student> studentList = StudentUtil.getStudentList();
System.out.println("排序之前:");
studentList.stream().forEach(System.out::println);
studentList.sort(new Comparator<Student>() {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.getAge()-o2.getAge();
    }
});
System.out.println("排序之后:");
studentList.stream().forEach(System.out::println);
}
Comparator接口(函数式接口)中只有compare一个方法,我们实现定制排序的重点在于compare一个方法,因此,在实现上,可以直接考虑实现这个方法,而不用生成匿名实现类的对象,Lambda表达式就是基于此产生的,下面的代码是基于Lambda表达式生成的:
 List<Student> studentList = StudentUtil.getStudentList();
 System.out.println("排序之前:");
 studentList.stream().forEach(System.out::println);
 studentList.sort((s1, s2) -> s1.getAge()-s2.getAge());
 System.out.println("排序之后:");
 studentList.stream().forEach(System.out::println);
代码的运行结果都是一致的

在这里插入图片描述

语法

由于Lambda表达式相当于要以一个表达式,来替换一个方法的匿名实现类的对象,Lambda中包含了三部分,参数列表、(Lambda操作符)->,Lambda体,相当于涵盖了一个方法的参数列表和方法体,不包含方法名,因此,Lambda替换的匿名实现类的对象只能是包含一个抽象方法,即对应的接口必须是函数式接口

Lambda表达式的形式如下:

(参数1,参数2,参数3) -> {
方法实现;
return xxx;(可以没有)
}
当参数只有一个是,可以不加(),
当Lambda只有一行代码时可以省略{},如果只包含return xxx;(可以没有),将return也省略
注意,不用写参数类型,可以自动推断

针对四大函数式接口的Lambda表达式实现

Consumer接口

@Test
public void test3(){
     test(s-> System.out.println(s));
 }

 public void test(Consumer<String> consumer){
     consumer.accept("hello");
 }

Supplier接口

@Test
public void test4(){
    System.out.println(test(() -> "hello world"));
}

public String test(Supplier<String> supplier){
    return supplier.get();
}

Function接口

@Test
public void test4(){
    System.out.println(test(s->s.length()));
}

public Integer test(Function<String,Integer> function){
    return function.apply("hello");
}

Predicate接口

@Test
public void test4(){
    System.out.println(test(s->s.equals("hello")));
}

public boolean test(Predicate<String> predicate){
    return predicate.test("hello");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

今天不coding

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值