如何使用同时使用键和值的比较器对树形图进行排序(Java)

问题描述 投票:0回答:1

所以我在这个类中尝试使用我在另一个类(单独的文件)中创建的比较器来初始化 SortedMap

为什么这部分不起作用?

Comparator<Map.Entry<Country, Pair<Integer, Integer>>> comparator = new SortCountryChargers();
countryChargers = new TreeMap<Country, Pair<Integer, Integer>>(comparator);

文件1:

public class Ex4 {

    private static SortedMap<Country, Pair<Integer, Integer>> countryChargers;
    
    public static void setCountryChargers(Set<ChargingStation> chargingStationSet, int Kw){
        Comparator<Map.Entry<Country, Pair<Integer, Integer>>> comparator = new SortCountryChargers();
        countryChargers = new TreeMap<Country, Pair<Integer, Integer>>(comparator);
    
        for(ChargingStation chargingStation : chargingStationSet){
            // get the charging station's country
            Country country = chargingStation.getCountry();
    
            // check if the country is already part of the hashmap
            // if not, add it
            if(!countryChargers.containsKey(country)){
                Pair<Integer, Integer> newPair = new Pair<>(0,0);
                countryChargers.put(country, newPair);
            }
    
            // update the hashmap
            // the first member of the pair is the charging stations > kw
            // the second member is the charging stations <= kw
            // first + second = total
            if(chargingStation.getkW() > Kw){
                int increment = countryChargers.get(country).getFirst() + 1 ;
                countryChargers.get(country).setFirst(increment);
            } else {
                int increment = countryChargers.get(country).getSecond() + 1 ;
                countryChargers.get(country).setSecond(increment);
            }
        }
    }

}

文件2:

public class SortCountryChargers implements Comparator<Map.Entry<Country, Pair<Integer, Integer>>> {

public int compare(Map.Entry<Country,Pair<Integer, Integer>> object1, Map.Entry<Country,Pair<Integer, Integer>> object2){

        //get the total charging station num for objects 1 and 2
        int pair1 = sumPairs(object1.getValue());
        int pair2 = sumPairs(object2.getValue());
    
        //compare total chargig station num
        if(pair1 > pair2) return 1;
        else if (pair1 < pair2) return -1;
    
        //if the total charging station num is equal, compare country names
        String country1 = object1.getKey().getName();
        String country2 = object2.getKey().getName();
        return country1.compareTo(country2);
    }
    
    //get the sum of the two members of an <integer, interger> pair
    public int sumPairs(Pair<Integer, Integer> p){
        return p.getFirst() + p.getSecond();
    }

}

我尝试阅读多篇文章,但到目前为止我还没有找到答案。如果可以的话请帮帮我!

java hashmap comparator treemap sortedmap
1个回答
0
投票

A

TreeMap
只能 根据其键进行排序,而不是根据其条目进行排序。您正在尝试使用 Comparator<Map.Entry<County, Pair<Integer, Integer>>>
,但这不是 
TreeMap
 的工作原理。

如果您想根据键和值对条目进行排序,则无法在地图中内置这样做;您必须在地图的

entrySet

 上单独进行操作。

© www.soinside.com 2019 - 2024. All rights reserved.