Java中的HashMap值排序不回正确的顺序
我在排序HashMaps
与Java
值有一些问题。 我的代码是:Java中的HashMap值排序不回正确的顺序
@SuppressWarnings("unchecked")
Map<String, Integer> scores = ((HashMap<String, Integer>) prefs.get());
Map<String, Integer> sortedscores = sortByValues(scores);
printMap(scores);
System.out.println("==============");
printMap(sortedscores);
的prefs.get()返回一个Map<String, ?>
我转换为<String, Integer >
分拣功能:
public static <K, V extends Comparable<V>> Map<K, V> sortByValues(final Map<K, V> map) {
Comparator<K> valueComparator = new Comparator<K>() {
public int compare(K k1, K k2) {
int compare = map.get(k2).compareTo(map.get(k1));
if (compare == 0) return 1;
else return compare;
}
};
Map<K, V> sortedByValues = new TreeMap<K, V>(valueComparator);
sortedByValues.putAll(map);
return new LinkedHashMap<K,V>(sortedByValues);
}
public static void printMap(Map<String, Integer> unsortMap){
for (Map.Entry entry : unsortMap.entrySet()) {
System.out.println("Key : " + entry.getKey()
+ " Value : " + entry.getValue());
}
}
的输出是:
Key : John Doe Value : 1000
Key : balazs Value : 975
Key : Balazs Value : 900
Key : aladar Value : 975
Key : balazs2 Value : 975
Key : score Value : 1000
Key : house Value : 1037
==============
Key : balazs Value : 975
Key : aladar Value : 975
Key : balazs2 Value : 975
Key : Balazs Value : 900
Key : house Value : 1037
Key : John Doe Value : 1000
Key : score Value : 1000
第一个是未排序的,第二个是排序d。 我的问题是,第二输出不在DESC顺序(按价值计算)
编辑: 如果我创建一个hasmap自己正常工作:
Map<String, Integer> unsortMap = new HashMap<String, Integer>();
unsortMap.put("asd", 1);
unsortMap.put("asd2r1", 5);
unsortMap.put("house", 7);
unsortMap.put("3", 124);
unsortMap.put("7", 4);
unsortMap.put("5", 6);
unsortMap.put("6", 2);
unsortMap.put("8", 0);
但是,如果我有这样试试:Map<String, Integer> scores = ((HashMap<String, Integer>) prefs.get());
我得到那个奇怪的命令。
你比较看起来并不像它符合specification:
int compare = map.get(k2).compareTo(map.get(k1));
if (compare == 0) return 1;
else return compare;
你为什么返回1当两个条目是平等的吗?
尝试过,但没有发生,输出是一样的 – user1601401 2013-03-27 13:55:38
我编辑了我的问题,请再次看到它 – user1601401 2013-03-27 13:59:31
你应该基本上重写此:
public int compare(K k1, K k2) {
int compare = map.get(k2).compareTo(map.get(k1));
if (compare == 0) return 1;
else return compare;
}
到:
public int compare(K k1, K k2) {
return map.get(k2).compareTo(map.get(k1));
}
当两个值相等时,你实际上是在说一个比另一个更大......这没有按”没有什么意义。如果密钥可比较,则使用自然比较。
我已经尝试过,但输出是相同的,除了缺少重复项 – user1601401 2013-03-27 13:54:34
我认为这个问题是在回一句:
Map<K, V> sortedByValues = new TreeMap<K, V>(valueComparator);
sortedByValues.putAll(map);
return new LinkedHashMap<K,V>(sortedByValues);
你有一个排序的映射,然后你把所有的对在新的地图,使他们获得无序一次。试着这样做:
Map<K, V> sortedByValues = new TreeMap<K, V>(valueComparator);
sortedByValues.putAll(map);
return sortedByValues;
找到了解决办法: 的问题是,它比较字符串不是整数。我试过的转换
Map<String, Integer> scores = ((HashMap<String, Integer>) prefs.get());
没有将其转换为整数。 所以我用一个周期,做正确的事:
for (@SuppressWarnings("rawtypes") Map.Entry entry : scores.entrySet()) {
scoresInt.put(entry.getKey().toString(), Integer.parseInt(entry.getValue().toString()));
}
随着hasmap被转换为分拣工作就像一个魅力。
好的,这里有什么问题? – midhunhk 2013-03-27 13:50:32
值应该排序DESC顺序,但它是975,975,975,900,1037,1000,1000,1037不是它应该在的位置 – user1601401 2013-03-27 13:51:11
如果您的排序顺序搞乱了,比较器看起来像是错了。 – midhunhk 2013-03-27 13:53:25