使用可以访问所述TreeMap的比较器实例化TreeMap [英] Instantiating a TreeMap using a Comparator which should be able to access the said TreeMap

查看:151
本文介绍了使用可以访问所述TreeMap的比较器实例化TreeMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用比较器来实例化一个 TreeMap 可以访问所述 TreeMap ,即它将 使用的(我猜必须精确地是问题...):

I'm trying to instantiate a TreeMap using a Comparator which should be able to access the said TreeMap, i.e. the one it will be used for (I guess that "will" must precisly be the problem...):

final Map<String, Integer> map = new TreeMap<String, Integer>(new Comparator<String>() {

    @Override
    public int compare(String o1, String o2) {
        Integer i = map.get(o1);
        // Error: "the local variable map may not have been initialized"
        return ...;
    }

});

我可以了解为什么会出现此错误,因为实例化 Comparator< String> ; map 变量尚未初始化,但是是否有任何解决方法?

I can get why this error occurs, since when instantiating the Comparator<String>, the map variable isn't initialized yet, but is there any workaround solution?

解决方案将是 setComparator 方法java / util / TreeMap.java.htmlrel =nofollow> TreeMap 实现,但其比较器字段已被声明为final:

A solution would have been a setComparator method in the TreeMap implementation, but its comparator field has been declared as final:

final Map<String, Integer> map = new TreeMap<String, Integer>();
Comparator<String> comparator = new Comparator<String>() {

    @Override
    public int compare(String o1, String o2) {
        Integer i = map.get(o1);
        return ...;
    }

};
// map.setComparator(comparator);


推荐答案

两个月后在答案@ hd1给了我(这不起作用 )后,实际上),这是一个可能的解决方案:

Two months later, after having relooked into the answer @hd1 gave me (which doesn't work as is actually), here is a possible solution:

public class SortedByValueMap<K, V extends Comparable<V>> implements Comparator<K> {

    private final Map<K, V> map = new TreeMap<K, V>();

    private class CustomTreeMap<KK extends K, VV extends V> extends TreeMap<KK, VV> {

        private static final long serialVersionUID = 9196929305071517886L;

        private CustomTreeMap(Comparator<KK> c) {
            super(c);
        }

        @Override
        public VV put(KK key, VV value) {
            map.put(key, value);
            return super.put(key, value);
        };

        @Override
        public VV remove(Object key) {
            map.remove(key);
            return super.remove(key);
        }

    }

    @Override
    public int compare(K o1, K o2) {
        return map.get(o1).compareTo(map.get(o2));
    }

    public Map<K, V> getMap() {
        return new CustomTreeMap<K, V>(this);
    }

}

然后:

Map<String, Integer> map = new SortedByValueMap<String, Integer>().getMap();
map.put("r", 2);
map.put("b", 0);
map.put("a", 1);
System.out.println(map); // prints {b=0, a=1, r=2}

但无论如何,被认为是技术挑战的解决方案,而不是真正有效的工具(因为创建了两个相同的并行映射),所以请谨慎使用... ;)

But anyway, it has to be considered as a technical challenge solution rather than a real efficient tool though (since two identical parallel maps are created), so use it sparingly... ;)

这篇关于使用可以访问所述TreeMap的比较器实例化TreeMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆