java根據成績排名 相同成績同名次

/**
 * 排序:把不同成績保存map中,最後實體類根據map的key找到排名 存入實體排名裏面,前端取值。
 * @return
 */
public static Map<Double,Integer> rankScore(List<DeductPersonVO> stus) {
    Map<Double,Integer> map = new HashMap<>();
    List<Map.Entry<Double, List<DeductPersonVO>>> list = stus.stream().collect(Collectors.groupingBy(DeductPersonVO::getDeductScore)).entrySet()
            .stream().sorted((s1, s2) -> -Double.compare(s1.getKey(), s2.getKey())).collect(Collectors.toList());
    int index = 1;
    for (Map.Entry<Double, List<DeductPersonVO>> entry : list) {

        map.put(entry.getKey(),index);
        entry.getValue().forEach((s) -> System.out.print("  " + s.getDeductScore()));
        System.out.println();
        index++;
    }
    return map;
}

取值存入實體

Map<Double,Integer> rankMap2 = rankScore(personList);
personList.stream().forEach(bean -> {
    bean.setRank(rankMap2.get(bean.getDeductScore()));
});

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章