栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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 list = new ArrayList() {{
            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;
        
        for (Person bean : list) {
            if (Double.compare(lastAge, bean.getAge()) != 0) {
                lastAge = bean.getAge();
                index++;
            }
            bean.setRank(index);
        }

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

        
//        Map 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结果示例

最后

欢迎指正

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/979179.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号