java8新特性(一)使用Lambda表达式集合比较排序

2018-01-19  本文已影响0人  dogiant

概述

最近开始在手头的项目中用到了一些java8的新特性,用了感觉不错,特分享记录下。
Lambda表达式,是java语言层次上的改变,Lambda允许把函数作为一个方法的参数(函数作为参数传递进方法中),或者把代码看成数据,一种函数式编程的概念。废话不多说,上代码:

在集合比较排序上的应用

我们先创建一个简单的实体类:

public class Staff {
    //ID作为唯一标识
    private Long id;
    private Integer age;
    private String name;
    public Staff() {
        super();
    }
    public Staff(Long id, Integer age, String name) {
        super();
        this.id = id;
        this.age = age;
        this.name = name;
    }
...
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Staff other = (Staff) obj;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return "Staff [id=" + id + ", age=" + age + ", name=" + name + "]";
    }

在java8以前对于对象排序,需要实现匿名比较器来做比较。

    List<Staff> list2 = Arrays.asList(
            new Staff(2L, 25, "王五"), 
            new Staff(1L, 18, "张三"), 
            new Staff(3L, 36, "李六"));
    
    Collections.sort(list2, new Comparator<Staff>(){
        @Override
        public int compare(Staff o1, Staff o2) {
            return o1.getAge().compareTo(o2.getAge());
        }
    });

    System.out.println(list2.get(0));

Staff [id=1, age=18, name=张三]

使用Lambda表达式实现

    @Test
    public void testLambdaSorted() {
        List<Staff> staffs = Arrays.asList(
                new Staff(2L, 25, "王五"), 
                new Staff(1L, 18, "张三"), 
                new Staff(3L, 36, "李六"));

        staffs.sort((Staff o1, Staff o2) -> o1.getAge().compareTo(o2.getAge()));

        assertThat(staffs.get(0), equalTo(new Staff(1L, 18, "张三")));
    }

我们可以省略Staff类的指定,编译器能帮自动推定。

staffs.sort((o1, o2) -> o1.getAge().compareTo(o2.getAge()));

    @Test
    public void testLambdaSorted2() {
        List<Staff> staffs = Arrays.asList(
                new Staff(2L, 25, "王五"), 
                new Staff(1L, 18, "张三"), 
                new Staff(3L, 36, "李六"));

        staffs.sort((o1, o2) -> o1.getAge().compareTo(o2.getAge()));

        assertThat(staffs.get(0), equalTo(new Staff(1L, 18, "张三")));
    }

要翻转排序?

    @Test
    public void testLambdaSortReversed() {
        List<Staff> staffs = Arrays.asList(
                new Staff(2L, 25, "王五"), 
                new Staff(1L, 18, "张三"), 
                new Staff(3L, 36, "李六"));
        Comparator<Staff> comparator = (o1, o2) -> o1.getAge().compareTo(o2.getAge());
        staffs.sort(comparator.reversed());
        assertThat(staffs.get(0), equalTo(new Staff(1L, 18, "张三")));
    }

可以直接根据类的getter方法来排序

    @Test
    public void testLambdaSortedByGetter() {
        List<Staff> staffs = Arrays.asList(
                new Staff(2L, 25, "王五"), 
                new Staff(1L, 18, "张三"), 
                new Staff(3L, 36, "李六"));
        Collections.sort(staffs, Comparator.comparing(Staff::getAge));
        assertThat(staffs.get(0), equalTo(new Staff(1L, 18, "张三")));
    }

多条件排序?写一个根据名字排序,同名则再比较年龄的例子:

    @Test
    public void testLambdaSortedByMultipleCondition() {
        List<Staff> staffs = Arrays.asList(
                new Staff(2L, 25, "王五"), 
                new Staff(1L, 18, "张三"), 
                new Staff(4L, 17, "张三"),
                new Staff(3L, 36, "李六"));
        
        staffs.sort((lo, ro) -> {
            if (lo.getName().equals(ro.getName())) {
                return lo.getAge() - ro.getAge();
            } else {
                return lo.getName().compareTo(ro.getName());
            }
        });
        
        assertThat(staffs.get(0), equalTo(new Staff(4L, 17, "张三")));
    }

使用静态方法排序

1、先在Staff新建一个静态方法:

    public static int compareByNameThenAge(Staff lo, Staff ro) {
        if (lo.name.equals(ro.name)) {
            return lo.age - ro.age;
        } else {
            return lo.name.compareTo(ro.name);
        }
    }

2、然后就可以用这个方法进行排序了

staffs.sort(Staff :: compareByNameThenAge);

    @Test
    public void testLambdaSortedByStaticMethod() {
        List<Staff> staffs = Arrays.asList(
                new Staff(2L, 25, "王五"), 
                new Staff(1L, 18, "张三"), 
                new Staff(4L, 17, "张三"),
                new Staff(3L, 36, "李六"));
        staffs.sort(Staff :: compareByNameThenAge);
        assertThat(staffs.get(0), equalTo(new Staff(4L, 17, "张三")));
    }

不愿写这个多条件比较方法?可以,JAVA8开始Comparator支持链式排序

staffs.sort(Comparator.comparing(Staff::getName).thenComparing(Staff::getAge));

    @Test
    public void testLambdaSortedByChainGetter() {
        List<Staff> staffs = Arrays.asList(
                new Staff(2L, 25, "王五"), 
                new Staff(1L, 18, "张三"), 
                new Staff(4L, 17, "张三"),
                new Staff(3L, 36, "李六"));
        staffs.sort(Comparator.comparing(Staff::getName).thenComparing(Staff::getAge));
        assertThat(staffs.get(0), equalTo(new Staff(4L, 17, "张三")));
    }

结语:

这篇文章演示了一个列表排序可以使用java 8 Lambda表达式->的语法糖,以及如何实现更加简洁明了的语义排序代码。

Github

上一篇 下一篇

猜你喜欢

热点阅读