java 对于并列排名的实现

import java.util.*;
import java.util.Map.Entry;
import java.util.stream.Collectors;


class Student {
    private String name;
    private double score;
    private int age;
    private int index;

    public String getName() {
        return name;
    }

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

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    public int getAge() {
        return age;
    }

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

    public Student(String name, double score, int age) {
        this.name = name;
        this.score = score;
        this.age = age;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", score=" + score +
                ", age=" + age +
                ", index=" + index +
                '}';
    }
}

class TestDemo {
    public static void main(String[] args) {
        List<Student> students = Arrays.asList(
                new Student("Jack", 52, 10),
                new Student("Amdy", 79.5, 10),
                new Student("Lucy", 68, 9),
                new Student("Tom", 79.5, 11),
                new Student("Jerry", 52, 8),
                new Student("Cherry", 79.5, 10),
                new Student("Sweet", 91, 12),
                new Student("Lucky", 68, 9),
                new Student("Sim", 79.5, 10),
                new Student("Solem", 65, 10)
        );
        // fun1(students);
        // System.out.println("---------------分割线---------------------");
        // fun2(students);
        // System.out.println("---------------分割线---------------------");
        // fun3(students);
        // System.out.println("---------------分割线---------------------");
        func4(students);
    }

    /**
     * 按照成绩排序 ---低级写法
     */
    public static void fun1(List<Student> students) {
        students.sort((s1, s2) -> -Double.compare(s1.getScore(), s2.getScore()));
        int index = 0;
        double lastScore = -1;

        for (Student s : students) {
            if (Double.compare(lastScore, s.getScore()) != 0) {
                lastScore = s.getScore();
                index++;
            }
            System.out.println("名次:" + index + "\t分数" + s.getScore() + "\t名字" + s.getName());
        }
    }

    /**
     * 按照成绩排序 --- 使用Java 8
     */
    public static void fun2(List<Student> students) {
        List<Entry<Double, List<Student>>> list = students.stream()
                .collect(Collectors.groupingBy(Student::getScore))
                .entrySet()
                .stream()
                .sorted((s1, s2) -> -Double.compare(s1.getKey(), s2.getKey()))
                .collect(Collectors.toList());
        int index = 1;
        for (Entry<Double, List<Student>> entry : list) {
            System.out.print("名次:" + index + "\t分数:" + entry.getKey() + "\t名字");
            entry.getValue().forEach((s) -> System.out.print("  " + s.getName()));
            System.out.println();
            index++;
        }
    }

    /**
     * 按照成绩排序 --- 使用Java 8;并列排名跳到下一名
     */
    public static void fun3(List<Student> students) {
        List<Entry<Double, List<Student>>> list = students.stream()
                .collect(Collectors.groupingBy(Student::getScore))
                .entrySet()
                .stream()
                .sorted((s1, s2) -> -Double.compare(s1.getKey(), s2.getKey()))
                .collect(Collectors.toList());
        int index = 1;
        for (Entry<Double, List<Student>> entry : list) {
            System.out.print("名次:" + index + "\t分数:" + entry.getKey() + "\t名字");
            entry.getValue().forEach((s) -> System.out.print("  " + s.getName()));
            System.out.println();
            index = index + entry.getValue().size();
        }
    }

    /**
     * 按照多条件排序 --- 使用Java 8;并列排名跳到下一名
     */
    public static void func4(List<Student> students) {
        // students.sort((h1, h2) -> {
        //     // 排名相同,年龄正序排序
        //     if (Double.compare(h1.getScore(), h2.getScore()) == 0) {
        //         return Double.compare(h1.getAge(), h2.getAge());
        //     }
        //     return -Double.compare(h1.getScore(), h2.getScore());
        // });
        students.sort(Comparator.comparing(Student::getScore).reversed().thenComparing(Student::getAge));
        // students.forEach(System.out::println);
        int index = 0;
        int count = 0;
        double lastScore = -1;
        Map<Integer, Student> rankMap = new HashMap<>(5);
        for (int i = 0; i < students.size(); i++) {
            Student s = students.get(i);
            System.out.println(s.toString() + ",lastScore:" + lastScore + ",count:" + count + ",index:" + index);
            // 如果成绩和上一名的成绩不相同,那么排名+1
            if (Double.compare(lastScore, s.getScore()) != 0) {
                lastScore = s.getScore();
                index = index + 1 + count;
                count = 0;
            } else {
                // 分数相同,如果年龄不同,排名+1
                if (Double.compare(students.get(i - 1).getAge(), s.getAge()) != 0) {
                    index = index + 1 + count;
                    count = 0;
                } else {
                    // 重复数+1
                    count++;
                }
            }
            s.setIndex(index);
            System.out.println(s.toString() + ",lastScore:" + lastScore + ",count:" + count + ",index:" + index);
            System.out.println("****************");
            rankMap.put(i, s);
        }
        for (Integer key : rankMap.keySet()) {
            System.out.println(rankMap.get(key));
        }
    }

}
  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值