Home  >  Article  >  Java  >  Java uses lambda expression to sort a certain attribute of List object collection

Java uses lambda expression to sort a certain attribute of List object collection

王林
王林forward
2019-12-14 12:00:037655browse

Java uses lambda expression to sort a certain attribute of List object collection

Create a new UserInfo object here to test lambda expression sorting. The properties are as follows:

public class UserInfo {
    private int id;
    private int age;
    private String name;

    public UserInfo(int id, int age, String name) {
        this.id = id;
        this.age = age;
        this.name = name;
    }

    public int getId() {
        return id;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }测试

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

Recommended related video tutorials: java learning

Start creating data, and sort it in positive order by age during the test:

 public static void main(String[] args) {
        UserInfo user1 = new UserInfo(1, 20, "赵");
        UserInfo user2 = new UserInfo(2, 25, "钱");
        UserInfo user3 = new UserInfo(3, 23, "孙");
        UserInfo user4 = new UserInfo(7, 16, "李");
        UserInfo user5 = new UserInfo(6, 19, "周");
        UserInfo user6 = new UserInfo(5, 12, "吴");
        List<UserInfo> userInfoList = new ArrayList<>();
        userInfoList.add(user2);
        userInfoList.add(user1);
        userInfoList.add(user3);
        userInfoList.add(user4);
        userInfoList.add(user5);
        userInfoList.add(user6);
        //使用lambda表达式对age大小正序排序
        userInfoList = userInfoList.stream().sorted(Comparator.comparing(UserInfo::getAge))
        .collect(Collectors.toList());
        for (UserInfo userInfo : userInfoList) {
            System.out.println(userInfo.toString());
        }
    }

The output results are as follows:

Java uses lambda expression to sort a certain attribute of List object collection

The test is sorted in reverse order by age. :

public static void main(String[] args) {
        UserInfo user1 = new UserInfo(1, 20, "赵");
        UserInfo user2 = new UserInfo(2, 25, "钱");
        UserInfo user3 = new UserInfo(3, 23, "孙");
        UserInfo user4 = new UserInfo(7, 16, "李");
        UserInfo user5 = new UserInfo(6, 19, "周");
        UserInfo user6 = new UserInfo(5, 12, "吴");
        List<UserInfo> userInfoList = new ArrayList<>();
        userInfoList.add(user2);
        userInfoList.add(user1);
        userInfoList.add(user3);
        userInfoList.add(user4);
        userInfoList.add(user5);
        userInfoList.add(user6);
           //使用lambda表达式对age大小倒序排序,发现就是多加了个reversed()方法
        userInfoList = userInfoList.stream().sorted(Comparator.comparing(UserInfo::getAge).
        reversed()).collect(Collectors.toList());
        for (UserInfo userInfo : userInfoList) {
            System.out.println(userInfo.toString());
        }
    }

The output results are as follows:

Java uses lambda expression to sort a certain attribute of List object collection

Summarize the sorting method:

1. Sort according to a certain field in positive order:

userInfoList.stream().sorted(Comparator.comparing(UserInfo::getAge)).collect(Collectors.toList());

2. Sort in reverse order according to a certain field:

userInfoList.stream().sorted(Comparator.comparing(UserInfo::getAge).reversed()).collect(Collectors.toList());

Just adjust the above parameters according to your business.

Recommended related articles and tutorials: Getting started with java

The above is the detailed content of Java uses lambda expression to sort a certain attribute of List object collection. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete