在hashmap内部的对象中进行值

  • 本文关键字:对象 hashmap 内部 java
  • 更新时间 :
  • 英文 :


我必须为自己的奖牌表编码自己的``填充'',我将其放入

HashMap <String, MedalsOfCountry> unsortedList

key是一个国家的代码,例如:"美国",MedalsOfCountry是具有字段的特定国家的对象:goldCountsilverCountbronzeCountMedalsOfCountry是一个静态内部类,我对此进行了编码,看起来像这样:

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;
    }

无论如何,我试图对这样的hashmap进行排序:

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));
        }

为什么我的排序不起作用?

treemap由键排序:

根据其键的自然顺序对地图进行排序,或者根据地图创建时间提供的比较器,具体取决于使用哪个构造函数。

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

通过插入顺序对LinkedHashMap进行排序:

此链接列表定义了迭代排序,通常是将密钥插入地图(插入订单)的顺序。

Map<String, MedalsOfCountry> sorted = unsorted.entrySet().stream()
    .sorted((entry0, entry1) -> entry0.getValue().compareTo(entry1.getValue()))
    .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v0, v1) -> {
        throw new IllegalArgumentException("duplicate keys are not possible here");
    }, LinkedHashMap::new));

为了改进这一点,我建议在您的对象类中实现可比接口:

static class MedalsOfCountry implements Comparable<MedalsOfCountry> {
    int goldCount;
    int silverCount;
    int bronzeCount;
    MedalsOfCountry(int count) {
        goldCount += count;
    }
    @Override
    public int compareTo(MedalsOfCountry medals) {
        int compareGold = this.goldCount - medals.goldCount;
        if (compareGold != 0) {
            return compareGold;
        }
        int compareSilver = this.silverCount - medals.silverCount;
        if (compareSilver != 0) {
            return compareSilver;
        }
        return this.bronzeCount - medals.bronzeCount;
    }
}

然后,您可以使用以下来创建排序的地图:

Map<String, MedalsOfCountry> sorted = unsorted.entrySet().stream()
    .sorted(Comparator.comparing(Map.Entry::getValue))
    .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v0, v1) -> {
        throw new IllegalArgumentException("duplicate keys are not possible here");
    }, LinkedHashMap::new));

此外,您可以通过使用比较器来定义多个步骤来改进compareTo()方法:

@Override
public int compareTo(MedalsOfCountry medals) {
    return Comparator.comparingInt(MedalsOfCountry::getGoldCount)
        .thenComparingInt(MedalsOfCountry::getSilverCount)
        .thenComparingInt(MedalsOfCountry::getBronzeCount)
        .compare(this, medals);
}

也可以使用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));

最新更新