Java 在HashMap内对象中的值之后排序

Java 在HashMap内对象中的值之后排序,java,Java,我必须为奖牌表编写自己的“过滤器”,并将其放入 HashMap未排序列表 键是一个国家的代码,例如:“USA”和MedalsOfCountry是特定国家的对象,其字段为:goldCount,silverCount,bronzeCountMedalsOfCountry是一个静态的内部类,我对其进行了编码,如下所示: static class MedalsOfCountry extends Filter { int goldCount; int silverCount; in

我必须为奖牌表编写自己的“过滤器”,并将其放入

HashMap未排序列表

是一个国家的代码,例如:“USA”和
MedalsOfCountry
是特定国家的对象,其字段为:
goldCount
silverCount
bronzeCount
MedalsOfCountry
是一个静态的内部类,我对其进行了编码,如下所示:

static class MedalsOfCountry extends Filter {
    int goldCount;
    int silverCount;
    int bronzeCount;
    Medal medalType;

    MedalsOfCountry(Medal m, int count) {
        medalType = m;
        if (medalType == Medal.GOLD) {
            goldCount += count;
        } else if (medalType == Medal.SILVER) {
            silverCount+= count;
        } else {
            bronzeCount += count;
        }
    }

    public int compareTo(MedalOfCountry medals) {
        if(this.goldCount == medals.goldCount) {
            return this.silverCount - medals.silverCount;
        } else if(this.silverCount == medals.silverCount) {
            return this.bronzeCount - medals.bronzeCount;
        }
        return this.goldCount - medals.goldCount;
    }
Map<String, MedalsOfCountry> sortedList = new TreeMap<String, MedalsOfCountry>(new Comparator<String>() {
        @Override
        public int compare(String land1, String land2) {
            return unsortedList.get(land1).compareTo(unsortedList.get(land2));
        }
无论如何,我尝试按如下方式对HashMap进行排序:

static class MedalsOfCountry extends Filter {
    int goldCount;
    int silverCount;
    int bronzeCount;
    Medal medalType;

    MedalsOfCountry(Medal m, int count) {
        medalType = m;
        if (medalType == Medal.GOLD) {
            goldCount += count;
        } else if (medalType == Medal.SILVER) {
            silverCount+= count;
        } else {
            bronzeCount += count;
        }
    }

    public int compareTo(MedalOfCountry medals) {
        if(this.goldCount == medals.goldCount) {
            return this.silverCount - medals.silverCount;
        } else if(this.silverCount == medals.silverCount) {
            return this.bronzeCount - medals.bronzeCount;
        }
        return this.goldCount - medals.goldCount;
    }
Map<String, MedalsOfCountry> sortedList = new TreeMap<String, MedalsOfCountry>(new Comparator<String>() {
        @Override
        public int compare(String land1, String land2) {
            return unsortedList.get(land1).compareTo(unsortedList.get(land2));
        }
Map sortedList=新树映射(新比较器(){
@凌驾
公共整数比较(字符串land1、字符串land2){
返回unsortedList.get(land1.compareTo)(unsortedList.get(land2));
}
为什么我的排序不起作用?

A按键排序:

映射根据其键的自然顺序进行排序,或者由映射创建时提供的比较器进行排序,具体取决于使用的构造函数

因此,在您的情况下,使用树状图并不是最佳选择。您可以使用
LinkedHashMap
或将地图更改为
列表

A按插入顺序排序:

此链表定义了迭代顺序,通常是将关键帧插入贴图的顺序(插入顺序)

To还可以使用
Comparator.reversed()
方法反转流的顺序:

Map<String, MedalsOfCountry> sorted = unsorted.entrySet().stream()
    .sorted(Comparator.comparing(Map.Entry<String, MedalsOfCountry>::getValue).reversed())
    .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v0, v1) -> {
        throw new IllegalArgumentException("duplicate keys are not possible here");
    }, LinkedHashMap::new));
Map sorted=unsorted.entrySet().stream()
.sorted(Comparator.comparing(Map.Entry::getValue).reversed())
.collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue,(v0,v1)->{
抛出新的IllegalArgumentException(“此处不可能重复密钥”);
},LinkedHashMap::new));
A按键排序:

映射根据其键的自然顺序进行排序,或者由映射创建时提供的比较器进行排序,具体取决于使用的构造函数

因此,在您的情况下,使用树状图并不是最佳选择。您可以使用
LinkedHashMap
或将地图更改为
列表

A按插入顺序排序:

此链表定义了迭代顺序,通常是将关键帧插入贴图的顺序(插入顺序)

To还可以使用
Comparator.reversed()
方法反转流的顺序:

Map<String, MedalsOfCountry> sorted = unsorted.entrySet().stream()
    .sorted(Comparator.comparing(Map.Entry<String, MedalsOfCountry>::getValue).reversed())
    .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v0, v1) -> {
        throw new IllegalArgumentException("duplicate keys are not possible here");
    }, LinkedHashMap::new));
Map sorted=unsorted.entrySet().stream()
.sorted(Comparator.comparing(Map.Entry::getValue).reversed())
.collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue,(v0,v1)->{
抛出新的IllegalArgumentException(“此处不可能重复密钥”);
},LinkedHashMap::new));

您正在尝试按国家(键)对其进行排序或者是
MedalsOfCountry
?一个树状图有其键排序,而不是其值。如果您想按值排序,请创建一个列表,并对该列表进行排序。请注意,MedalsOfCountry类没有多大意义,因为它应该计算一个国家的金牌、银牌和铜牌数量,但无法增加计数:goldCount、silverCount或bronzeCount都将等于1,其他的都将始终等于0。在一个类中设置一个medalType字段来计算这三种奖牌也没有多大意义。我尝试按
MedalsOfCountry
对其进行排序,例如,如果“美国”有46枚金牌,“中国”如果你想按照一个国家的奖牌数量来排序,你首先应该考虑如何对它进行排序。如果只是按照金牌数量来排序,还是按照所有的奖牌来排序。如果所有类型的奖牌你应该考虑给奖牌类型加一个砝码。因此,根据奖牌类型、重量和数量,你可以定义一个排序标准。你的<代码> MealAlfStand 很可能应该使用构造函数如<代码>新的奖章国家(金牌、银牌、铜牌)来初始化。
并为每种奖牌类型设置getter,如
getgoldcarges
等。通过这种方式,您可以编写比较器,如
comparator.comparingit(MedalsOfCountry::getgoldcarges)。然后是comparingit(MedalsOfCountry::getSilvercarges)。然后是comparingit(MedalsOfCountry::getBronzeMedals)
。此比较器将首先测试金牌数量,然后如果金牌数量相同,则测试银牌数量,如果银牌数量相同,则检查铜牌数量。您正在尝试按国家(键)对其进行排序或者是
MedalsOfCountry
?一个树状图有其键排序,而不是其值。如果您想按值排序,请创建一个列表,并对该列表进行排序。请注意,MedalsOfCountry类没有多大意义,因为它应该计算一个国家的金牌、银牌和铜牌数量,但无法增加计数:goldCount、silverCount或bronzeCount都将等于1,其他的都将始终等于0。在一个类中设置一个medalType字段来计算这三种奖牌也没有多大意义。我尝试按
MedalsOfCountry
对其进行排序,例如,如果“美国”有46枚金牌,“中国”如果你想按照一个国家的奖牌数量来排序,你首先应该考虑如何对它进行排序。如果只是按照金牌数量来排序,还是按照所有的奖牌来排序。如果所有类型的奖牌你应该考虑给奖牌类型加一个砝码。因此,根据奖牌类型、重量和数量,你可以定义一个排序标准。你的<代码> MealAlfStand 很可能应该使用构造函数如<代码>新的奖章国家(金牌、银牌、铜牌)来初始化。
并为每种奖牌类型提供获取者,如
获取金牌Map<String, MedalsOfCountry> sorted = unsorted.entrySet().stream()
    .sorted(Comparator.comparing(Map.Entry<String, MedalsOfCountry>::getValue).reversed())
    .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v0, v1) -> {
        throw new IllegalArgumentException("duplicate keys are not possible here");
    }, LinkedHashMap::new));