奖牌榜排序

知识点

stream的sorted排序,按照顺序进行排序,先进行铜牌排序,再进行银牌排序,最后进行金牌排序。

MedalInfo.java

public class MedalInfo {

    /**
     * 国家名称
     */
    private String countryName;

    /**
     * 金牌数量
     */
    private int goldNumber;

    /**
     * 银牌数量
     */
    private int sliverNumber;

    /**
     * 铜牌数量
     */
    private int bronzeNumber;

    public String getCountryName() {
        return countryName;
    }

    public void setCountryName(String countryName) {
        this.countryName = countryName;
    }

    public int getGoldNumber() {
        return goldNumber;
    }

    public void setGoldNumber(int goldNumber) {
        this.goldNumber = goldNumber;
    }

    public int getSliverNumber() {
        return sliverNumber;
    }

    public void setSliverNumber(int sliverNumber) {
        this.sliverNumber = sliverNumber;
    }

    public int getBronzeNumber() {
        return bronzeNumber;
    }

    public void setBronzeNumber(int bronzeNumber) {
        this.bronzeNumber = bronzeNumber;
    }

    @Override
    public String toString() {
        return "MedalInfo{" +
                "countryName='" + countryName + '\'' +
                ", goldNumber=" + goldNumber +
                ", sliverNumber=" + sliverNumber +
                ", bronzeNumber=" + bronzeNumber +
                '}';
    }
}

SortMain.java

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * @author :lichuankang
 * @date :Created in 2022/2/9 13:57
 * @description :
 */
public class SortMain {

    public static void main(String[] args) {
        List<MedalInfo> medalInfoList = buildMedalInfo();
        System.out.println("------------------排序前---------------");
        medalInfoList.forEach(System.out::println);
        System.out.println("------------------排序后---------------");
        medalInfoList.stream()
                .sorted(Comparator.comparing(MedalInfo::getBronzeNumber).reversed())
                .sorted(Comparator.comparing(MedalInfo::getSliverNumber).reversed())
                .sorted(Comparator.comparing(MedalInfo::getGoldNumber).reversed())
                .forEach(System.out::println);

    }

    private static List<MedalInfo> buildMedalInfo() {
        List<MedalInfo> medalInfos = new ArrayList<>();

        MedalInfo info = new MedalInfo();
        info.setCountryName("中国");
        info.setGoldNumber(3);
        info.setSliverNumber(2);
        info.setBronzeNumber(0);
        medalInfos.add(info);
        info = new MedalInfo();
        info.setCountryName("瑞士");
        info.setGoldNumber(4);
        info.setSliverNumber(3);
        info.setBronzeNumber(0);
        medalInfos.add(info);
        info = new MedalInfo();
        info.setCountryName("英国");
        info.setGoldNumber(2);
        info.setSliverNumber(1);
        info.setBronzeNumber(1);
        medalInfos.add(info);
        info = new MedalInfo();
        info.setCountryName("荷兰");
        info.setGoldNumber(2);
        info.setSliverNumber(1);
        info.setBronzeNumber(2);
        medalInfos.add(info);
        info = new MedalInfo();
        info.setCountryName("西班牙");
        info.setGoldNumber(4);
        info.setSliverNumber(2);
        info.setBronzeNumber(1);
        medalInfos.add(info);
        info = new MedalInfo();
        info.setCountryName("挪威");
        info.setGoldNumber(7);
        info.setSliverNumber(1);
        info.setBronzeNumber(4);
        medalInfos.add(info);

        return medalInfos;
    }
}

结果

奖牌榜排序

上一篇:2021/6/17爬虫第二十五次课(scrapy_reids补充)


下一篇:*static* abstract(抽象类)