旗下导航:搜·么
当前位置:网站首页 > JAVA教程 > 正文

java运用lambda表达式对List对象鸠合的某个属性举行排序【JAVA教程】,java,lambda,List,对象集合,属性,排序

作者:搜教程发布时间:2019-12-14分类:JAVA教程浏览:70评论:0


导读:这里新建一个UserInfo对象,用来测试lambda表达式排序,属性以下:publicclassUserInfo{privateintid;...

这里新建一个UserInfo对象,用来测试lambda表达式排序,属性以下:

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;
    }

相干视频教程引荐:java进修

入手下手造数据,测试下依据岁数大小正序排序:

 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());
        }
    }

输出效果以下:

测试依据岁数大小倒序排序:

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());
        }
    }

输出效果以下:

总结下排序运用要领:

1、依据某个字段正序排序:

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

2、依据某个字段倒序排序:

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

以上种种参数依据本身营业对号入座即可。

相干文章教程引荐:java入门进修

以上就是java运用lambda表达式对List对象鸠合的某个属性举行排序的细致内容,更多请关注ki4网别的相干文章!

标签:javalambdaList对象集合属性排序


欢迎 发表评论: