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

How do I sort a tree map using a comparator that uses both key and value

提问人:Docas 95 提问时间:10/6/2023 最后编辑:Mark RotteveelDocas 95 更新时间:10/6/2023 访问量:61

问:

所以我有这个类,我尝试使用我在另一个类(单独的文件)中创建的比较器来初始化 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 哈希 比较器 树状图 sortedmap

评论

1赞 Scott Hunter 10/6/2023
你有什么证据证明它“不起作用”?
0赞 Docas 95 10/6/2023
当我执行“countryChargers = new TreeMap<Country, Pair<Integer, Integer>>(comparator);”时,我得到“无法解析构造函数'TreeMap(Comparator<Entry<Country, Pair<Integer, Integer>>>)'”

答:

3赞 Louis Wasserman 10/6/2023 #1

A 只能根据其键进行排序,而不能根据其条目进行排序。您正在尝试使用 ,但这不是工作方式。(另一方面,它将接受 .)TreeMapComparator<Map.Entry<County, Pair<Integer, Integer>>>TreeMapComparator<Country>

如果要根据键和值对条目进行排序,则不能在映射中内置此操作;您必须在地图上单独执行此操作。entrySet

评论

0赞 Docas 95 10/6/2023
我到底该怎么做?我是否订购了 charginStation 套装,然后创建地图?我应该仍然使用树状图还是哈希图可以吗?我还需要比较器吗?
1赞 Louis Wasserman 10/6/2023
使用正常,a就可以了。然后,获取排序后的条目,例如 .MapHashMapmap.entrySet().stream().sorted(new SortCountryChargers()).collect(Collectors.toList())