Java 8 之 Stream sorted() List排序并排名

1.排序

1.1元素正排序

list = list.stream()
                .sorted(Comparator.comparing(Person::getAge))
                .collect(Collectors.toList());

1.2元素逆排序(reversed())

list = list.stream()
                .sorted(Comparator.comparing(Person::getAge).reversed())
                .collect(Collectors.toList());

2.List排名并获取名次示例

import lombok.Data;
import java.util.*;
import java.util.stream.Collectors;
public class RankTest {
    public static void main(String[] args) {
        List<Person> list = new ArrayList<Person>() {{
            add(new Person(10, "北京"));
            add(new Person(10, "西安"));
            add(new Person(20, "呼市"));
            add(new Person(20, "上海"));
            add(new Person(40, "南京"));
            add(new Person(60, "青海"));
        }};
        //从小到大正序
        list = list.stream()
                .sorted(Comparator.comparing(Person::getAge))
                .collect(Collectors.toList());
          //从大到小逆序
      //  list = list.stream()
        //        .sorted(Comparator.comparing(Person::getAge).reversed())
          //      .collect(Collectors.toList());
          //从大到小
//        list.sort((s1, s2) -> -Double.compare(s1.getAge(), s2.getAge()));

        int index = 0;
        double lastAge = -1;
        /**
         * 方法1: 出现并列排名后名次不间断
         */
        for (Person bean : list) {
            if (Double.compare(lastAge, bean.getAge()) != 0) {
                lastAge = bean.getAge();
                index++;
            }
            bean.setRank(index);
        }

        /**
         * 方法2.1: 出现并列排名后名次间断,后续名次按实际数据位置累计
         */
        //        for (int i = 0; i < list.size(); i++) {
//            if (list.get(i).getAge() == lastAge ){
//                list.get(i).setRank(index);
//                if (i + 1 < list.size() && list.get(i + 1).getAge() !=  lastAge){
//                    index = i + 1 ;
//                }
//            }else{
//                list.get(i).setRank(++index);
//            }
//            lastAge = list.get(i).getAge();
//        }

        /**
         * 方法2.2: 出现并列排名后名次间断,后续名次按实际数据位置累计(业务在name唯一的前提下使用)
         */
//        Map<String, Integer> rankMap = new HashMap<>();
//        for (int i = 1; i <= list.size(); i++) {
//            if (i != 1) {
//                if (list.get(i - 1).getAge().equals(list.get(i - 2).getAge())) {
//                    rankMap.put(list.get(i - 1).getName(), rankMap.get(list.get(i - 2).getName()));
//                } else {
//                    rankMap.put(list.get(i - 1).getName(), i);
//                }
//            } else {
//                rankMap.put(list.get(0).getName(), i);
//            }
//        }
//        for (Person person : list) {
//            person.setRank(rankMap.get(person.getName()));
//        }

        //打印结果
        list.forEach(p -> {
            System.out.println(p);
        });

    }
}

@Data
class Person {
    private Integer age;
    private Integer rank;
    private String name;

    public Person(Integer age, String name) {
        this.age = age;
        this.name = name;
    }

}

2.方法1结果示例

2.方法2.1结果示例

2.方法2.2结果示例

posted on   何苦->  阅读(1161)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 2 本地部署DeepSeek模型构建本地知识库+联网搜索详细步骤
历史上的今天:
2022-04-12 mysql 批量更新(插入)表字段 清洗数据 存储过程
2022-04-12 mysql 字符串匹配查询
2022-04-12 sql 让某字段查询结构显示为固定值

导航

< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示