【JAVA 8】 List 使用Lambda表达式操作对象字段进行升序、降序

总结

使用Lambda表达式轻松对集合进行排序
1、需要排序的对象implements Comparable 接口,并重写compareTo方法指明默认排序的字段
2、使用java 8的API轻松实现对List的排序

代码中也涉及了一些java 8的语法
嘻嘻,对新语法我也不熟悉

Main

public static void main(String[] args) {
    // 初始化集合
    List userList = new ArrayList();
    Random random = new Random();
    for (int i = 0; i < 15; i++) {
        userList.add(new User(
                random.nextInt(100),
                "user" + i,
                new Date(System.currentTimeMillis() - random.nextInt(100))));
    }
    // 原集合
    System.out.println("------userList------");
    userList.forEach((user) -> {
        System.out.println(user);
    });

    // 升序
    System.out.println("------ascByBirthdayList------");
    List ascByBirthdayList = userList.stream().sorted().collect(Collectors.toList());
    ascByBirthdayList.forEach((user) -> {
        System.out.println(user);
    });
    // 降序 --> 注意是在 ascByBirthdayList 的基础上,将 ascByBirthdayList 集合反转,更容易理解
    System.out.println("------descByBirthdayList------");
    List descByBirthdayList = ascByBirthdayList.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
    descByBirthdayList.forEach(user -> {
        System.out.println(user);
    });

    // 升序
    System.out.println("------ascByIdList------");
    List ascByIdList = userList.stream().sorted(Comparator.comparing(User::getId)).collect(Collectors.toList());
    ascByIdList.forEach(user -> {
        System.out.println(user);
    });
    /*
        降序 --> 注意这里不能在 ascByIdList 的基础上,将 ascByIdList 集合反转

        如果这里仅仅使用 ascByIdList.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
        那么得到的结果和 descByBirthdayList 是一致的, 因为没有指明排序的字段,默认是按birthday来排序的
     */
    System.out.println("------descByIdList------");
    List descByIdList = userList.stream().sorted(Comparator.comparing(User::getId).reversed()).collect(Collectors.toList());
    descByIdList.forEach(user -> {
        System.out.println(user);
    });

}

输出结果

------userList------
User{id=79, name='user0', birthday=1548384367374}
User{id=29, name='user1', birthday=1548384367339}
User{id=96, name='user2', birthday=1548384367401}
User{id=82, name='user3', birthday=1548384367375}
User{id=76, name='user4', birthday=1548384367352}
User{id=60, name='user5', birthday=1548384367385}
User{id=64, name='user6', birthday=1548384367313}
User{id=38, name='user7', birthday=1548384367352}
User{id=52, name='user8', birthday=1548384367308}
User{id=4, name='user9', birthday=1548384367333}
------ascByBirthdayList------
User{id=52, name='user8', birthday=1548384367308}
User{id=64, name='user6', birthday=1548384367313}
User{id=4, name='user9', birthday=1548384367333}
User{id=29, name='user1', birthday=1548384367339}
User{id=76, name='user4', birthday=1548384367352}
User{id=38, name='user7', birthday=1548384367352}
User{id=79, name='user0', birthday=1548384367374}
User{id=82, name='user3', birthday=1548384367375}
User{id=60, name='user5', birthday=1548384367385}
User{id=96, name='user2', birthday=1548384367401}
------descByBirthdayList------
User{id=96, name='user2', birthday=1548384367401}
User{id=60, name='user5', birthday=1548384367385}
User{id=82, name='user3', birthday=1548384367375}
User{id=79, name='user0', birthday=1548384367374}
User{id=76, name='user4', birthday=1548384367352}
User{id=38, name='user7', birthday=1548384367352}
User{id=29, name='user1', birthday=1548384367339}
User{id=4, name='user9', birthday=1548384367333}
User{id=64, name='user6', birthday=1548384367313}
User{id=52, name='user8', birthday=1548384367308}
------ascByIdList------
User{id=4, name='user9', birthday=1548384367333}
User{id=29, name='user1', birthday=1548384367339}
User{id=38, name='user7', birthday=1548384367352}
User{id=52, name='user8', birthday=1548384367308}
User{id=60, name='user5', birthday=1548384367385}
User{id=64, name='user6', birthday=1548384367313}
User{id=76, name='user4', birthday=1548384367352}
User{id=79, name='user0', birthday=1548384367374}
User{id=82, name='user3', birthday=1548384367375}
User{id=96, name='user2', birthday=1548384367401}
------descByIdList------
User{id=96, name='user2', birthday=1548384367401}
User{id=82, name='user3', birthday=1548384367375}
User{id=79, name='user0', birthday=1548384367374}
User{id=76, name='user4', birthday=1548384367352}
User{id=64, name='user6', birthday=1548384367313}
User{id=60, name='user5', birthday=1548384367385}
User{id=52, name='user8', birthday=1548384367308}
User{id=38, name='user7', birthday=1548384367352}
User{id=29, name='user1', birthday=1548384367339}
User{id=4, name='user9', birthday=1548384367333}

User 类

/**
 * 1、实现Comparable接口
 * 2、重写compareTo方法 默认按birthday排序
 */
public class User implements Comparable {

    private Integer id;
    private String name;
    private Date birthday;

    public User() {
    }

    public User(Integer id, String name, Date birthday) {
        this.id = id;
        this.name = name;
        this.birthday = birthday;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    @Override
    public int compareTo(User o) {
        return birthday.compareTo(o.getBirthday());
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", birthday=" + birthday.getTime() +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return id.equals(user.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }
}

你可能感兴趣的:(#,JAVA,8,系列)