java做成绩排名表,如何实现同分同名次

要求计算综合平均分

去除最高分和最低分,并且实现同分同名次

 

//去除最高分,和最低分,实现思路,

根据id 使用groupingby分组  然后循环分组的map   

接着根据分数进行排序,然后去除最高的和最低分,删除list 第一条数据,以及最后一条数,这样就把最高分,和最低分去除

 

然后统计平均分,实现思路,一样重新根据id 使用groupingby 重新分组

然后循环数据合并,根据id为key,相同id的数据 进行分数相加

    public List<CompetitionWorkExhibitionPo1> getList(List<CompetitionWorkExhibitionPo1> list) {
        List<CompetitionWorkExhibitionPo1> list1 = new ArrayList<>();
        List<CompetitionWorkExhibitionPo1> list2 = new ArrayList<>();

        Map<Long, List<CompetitionWorkExhibitionPo1>> collect = list.stream().collect(Collectors.groupingBy(CompetitionWorkExhibitionPo1::getCompetitionUserId));//使用groupingby分组
        for (Map.Entry<Long, List<CompetitionWorkExhibitionPo1>> com : collect.entrySet()) {
            List<CompetitionWorkExhibitionPo1> li = com.getValue();
            li.sort(Comparator.comparing(CompetitionWorkExhibitionPo1::getCompetitionUserId).thenComparing(CompetitionWorkExhibitionPo1::getSum));//根据分数排序
            li.remove(0);//删除list 第一条数据
            li.remove(li.size() - 1);//以及最后一条数
            list1.addAll(li);//然后把数据保存进一个新的list
        }
      Map
<Long, List<CompetitionWorkExhibitionPo1>> collect1 = list1.stream().collect(Collectors.groupingBy(CompetitionWorkExhibitionPo1::getCompetitionUserId));//统计平均分,重新分组 for (Map.Entry<Long, List<CompetitionWorkExhibitionPo1>> co : collect1.entrySet()) { List<CompetitionWorkExhibitionPo1> value = co.getValue(); int inx = value.size();//保存list的长度,用户计算平均分 list2.addAll(//处理完保存到一个新的集合 value.stream() // 表示id为key, 接着如果有重复的,那么从BillsNums对象o1与o2中筛选出一个,这里选择o1, // 并把id重复,需要将nums和sums与o1进行合并的o2, 赋值给o1,最后返回o1 .collect(Collectors.toMap(CompetitionWorkExhibitionPo1::getCompetitionUserId, a -> a, (o1, o2) -> { o1.setSum(o1.getSum() + o2.getSum());//分数相加 o1.setCount(String.valueOf(inx));//把list的长度,赋值 return o1; })).values().stream().collect(Collectors.toList()) ); }
int count = mapper2.count(); //查询最高点赞数 DecimalFormat df = new DecimalFormat("0.00");//格式化小数 for (CompetitionWorkExhibitionPo1 competitionWorkExhibitionPo1 : list2) {

        //处理点赞得分 competitionWorkExhibitionPo1.setGiveScore(competitionWorkExhibitionPo1.getPraiseCount()
> 0 ? df.format((float) competitionWorkExhibitionPo1.getPraiseCount() / count) : String.valueOf(0));
        //处理总分 competitionWorkExhibitionPo1.setReviewScore(df.format((
float) competitionWorkExhibitionPo1.getSum() / Integer.parseInt(competitionWorkExhibitionPo1.getCount()))); } for (CompetitionWorkExhibitionPo1 competitionWorkExhibitionPo1 : list2) {
        //处理综合总分=点赞得分+总分 competitionWorkExhibitionPo1.setComprehensiveScore(Double.valueOf(competitionWorkExhibitionPo1.getGiveScore())
+ Double.valueOf(competitionWorkExhibitionPo1.getReviewScore())); } List<CompetitionWorkExhibitionPo1> competitionWorkExhibitionPo1s = CompetitionRankingService.fun1(list2);//调用方法,得出排名return list2; }

 

 

// 方法一:传统的方法

排序:把不同成绩保存map中,最后实体类根据map的key找到排名 存入实体排名里面
  public static List<CompetitionWorkExhibitionPo1> fun1(List<CompetitionWorkExhibitionPo1> stus) {
        // 按照成绩排序
        stus.sort(new Comparator<CompetitionWorkExhibitionPo1>() {
            @Override
            public int compare(CompetitionWorkExhibitionPo1 s1, CompetitionWorkExhibitionPo1 s2) {
                return -Double.compare(s1.getComprehensiveScore(), s2.getComprehensiveScore());
            }
        });
        int rank = 1;//保存排名
        int index = 1;//排序号
        double lastScore = -1;// 最近一次的分

        for (int i = 0; i < stus.size(); i++) {
            CompetitionWorkExhibitionPo1 s = stus.get(i);
            if (Double.compare(lastScore, s.getComprehensiveScore()) != 0) { // 如果成绩和上一名的成绩不相同,那么排名+1
                lastScore = s.getComprehensiveScore();
                stus.get(i).setRank(rank++);//设置排名
                stus.get(i).setIndex(index++);//设置排序号
            }
        }
        stus.stream().forEach(System.out::println);
        return stus;
    }

/ 方法2: Java8开始支持的Lambada表达式配合 Stream API 来进行分组排序

  public static List<CompetitionWorkExhibitionPo1> fun2(List<CompetitionWorkExhibitionPo1> stus) {
        List<Map.Entry<Double, List<CompetitionWorkExhibitionPo1>>> list = stus.stream().collect(Collectors.groupingBy(CompetitionWorkExhibitionPo1::getComprehensiveScore)).entrySet()
                .stream().sorted((s1, s2) -> -Double.compare(s1.getKey(), s2.getKey())).collect(Collectors.toList());
        int rank = 1;//设置排名
        int index = 1;//设置排序号
        for (Map.Entry<Double, List<CompetitionWorkExhibitionPo1>> entry : list) {
            for (CompetitionWorkExhibitionPo1 competitionWorkExhibitionPo1 : entry.getValue()) {
                competitionWorkExhibitionPo1.setRank(rank++);
                competitionWorkExhibitionPo1.setIndex(index++);
            }
        }
        stus.stream().forEach(System.out::println);
        return stus;
    }

 

posted @ 2020-12-16 11:02  随★风  阅读(2316)  评论(0编辑  收藏  举报