我想根据一些价值归属来分类 Java树。 具体来说,我要根据<代码>的大小,分类如下:
<>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.