English 中文(简体)
在两个以上价值观相同的情况下,按价值计算出 Sor树马普
原标题:Sorting Java TreeMap by value not working when more than two values have the same sort-property

我想根据一些价值归属来分类 Java树。 具体来说,我要根据<代码>的大小,分类如下:。 Hashset<Integer>。 为此,我做了以下工作:

<>A 比较班:

private static class ValueComparer implements Comparator<Integer> {
    private Map<Integer, HashSet<Integer>>  map = null;
    public ValueComparer (Map<Integer, HashSet<Integer>> map){
        super();
        this.map = map;
    }

@Override
    public int compare(Integer o1, Integer o2) {
        HashSet<Integer> h1 = map.get(o1);
        HashSet<Integer> h2 = map.get(o2);

        int compare = h2.size().compareTo(h1.size());

        if (compare == 0 && o1!=o2){
            return -1;
        }
        else {
            return compare;
        }
    }
}

www.un.org/Depts/DGACM/index_spanish.htm 实例:

TreeMap<Integer, HashSet<Integer>> originalMap = new TreeMap<Integer, HashSet<Integer>>();

//load keys and values into map

ValueComparer comp = new ValueComparer(originalMap);
TreeMap<Integer, HashSet<Integer>> sortedMap = new TreeMap<Integer, HashSet<Integer>>(comp);
sortedMap.putAll(originalMap);

The problem:

页: 1 地图含有2个以上大小的数值。 在其他情况下,它就行使了权利。 当地图中有两个以上的数值相同时,新分类地图的第三数值为无效,在我试图查阅时投下Null PointerException。

I can t figure out what the problem is. Woule be nice if someone could point out.

Update: Here s an example that works when two values have the same size: http://ideone.com/iFD9c In the above example, if you uncomment lines 52-54, this code will fail- that s what my problem is.

最佳回答

Update: You cannot return -1 from ValueComparator just because you want to avoid duplicate keys to not be removed. Check the contract of Comparator.compare.


当你通过<条码>Comparator至,TreeMap时,你将一个(“新”)地点换成。 <>0>(computed) 钥匙可在中不止一次存在。 Tree/代码。


If you want to sort the orginalMap by size of the value you can do as follows:

public static void main(String[] args) throws Exception {

    TreeMap<Integer, HashSet<Integer>> originalMap = 
        new TreeMap<Integer, HashSet<Integer>>();

    originalMap.put(0, new HashSet<Integer>() {{ add(6); add(7); }});
    originalMap.put(1, new HashSet<Integer>() {{ add(6); }});
    originalMap.put(2, new HashSet<Integer>() {{ add(9); add(8); }});


    ArrayList<Map.Entry<Integer, HashSet<Integer>>> list = 
        new ArrayList<Map.Entry<Integer, HashSet<Integer>>>();
    list.addAll(originalMap.entrySet());

    Collections.sort(list, new Comparator<Map.Entry<Integer,HashSet<Integer>>>(){
        public int compare(Map.Entry<Integer, HashSet<Integer>> o1,
                           Map.Entry<Integer, HashSet<Integer>> o2) {

            Integer size1 = (Integer) o1.getValue().size();
            Integer size2 = (Integer) o2.getValue().size();
            return size2.compareTo(size1);
        }
    });

    System.out.println(list);
}
问题回答

你们的比较逻辑(我不相信我会照你为什么把固定规模相等但钥匙不同时的回归1)应该对地图本身在你称之为<条码>植被(关键)<>时的回报产生影响。

你们是否积极把无效价值插入最初的地图? 该法典如何看待?

您的参比人遵守比较合同:如果compare(o1, o2) < 0,则compare(o2, o1)应为> 0。 你们必须找到一种决定性的方法,在两者规模相同而且分类不相同的情况下,对你们的内容进行比较。 或许,你可以使用分类账的<代码>System.identityHashCode()来比较这种情况。

尽管如此,我确实想知道你可以做些什么:你可以制造新的愤怒,利用他们来从地图中获取价值,而且你可以修改它所拥有的建筑。

附带说明:贵国的比较代码样本使用<代码>>><>>>>>>和<代码>(<>数据>/代码>查询同一地图。

你们只能用钥匙订购树苗。 没有办法制造按价值订购的树苗,因为你们将获得斯纳克奥韦里的接受。

思考这个问题。 为了从树木中获取一个元素,你需要做比较,但为了进行比较,需要获得元素。

您将不得不在其他收集工作中加以分类,或使用 树木,你必须把 in(从切入值)也纳入切入钥匙,并使用从钥匙中吸收的那块树种定义参照器。

Assuming you cannot use a comparator that returns 0 with a Set, this might work: Add all the elements in originalMap.entrySet() to an ArrayList and then sort the ArrayList using your ValueComparer, changing it to return 0 as necessary.

然后在<代码>上添加所有条目:上。

我与最初的海报一样存在同样的问题。 我有一只树马普,希望以价值分类。 但是,当我担任一个参照国时,即由于JB所谈到的参照国的解体而存在问题。 我能够使用我的习惯比较器,并且仍然遵守合同。 当阀值一看是平等的,即回去比较钥匙。 如果价值观平等,我不关心这一秩序。

    public int compare(String a, String b) {
    if(base.get(a)[0] == base.get(b)[0]){ //need to handle when they are equal
        return a.compareTo(b);
    }else if (base.get(a)[0] < base.get(b)[0]) {
        return -1;
    } else {
        return 1;
    } // returning 0 would merge keys




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...