使用TreeMap的排序问题
我试图将一些键值放入 HashMap 中,然后尝试使用 TreeMap 进行排序,如下所示。问题是,如果映射中存在相似的值,那么排序后它会考虑其中的任何一个。
import java.util.*;
public class HashmapExample {
public static void main(String[] args) {
HashMap<String,Integer> map = new HashMap<String,Integer>();
ValueComparator bvc = new ValueComparator(map);
TreeMap<String,Integer> sorted_map = new TreeMap(bvc);
map.put("A",99);
map.put("B",67);
map.put("C",123);
map.put("G",67);
map.put("F",67);
map.put("H",67);
map.put("D",6);
System.out.println("unsorted map");
for (String key : map.keySet()) {
System.out.println("key/value: " + key + "/"+map.get(key));
}
sorted_map.putAll(map);
System.out.println("results after sorting");
for (String key : sorted_map.keySet()) {
System.out.println("key/value: " + key + "/"+sorted_map.get(key));
}
}
}
class ValueComparator implements Comparator {
Map base;
public ValueComparator(Map base) {
this.base = base;
}
public int compare(Object a,Object b) {
if((Integer)base.get(a) > (Integer)base.get(b)) {
return 1;
} else if((Integer)base.get(a) == (Integer)base.get(b)) {
return 0;
} else {
return -1;
}
}
}
之后的输出如下所示,
unsorted map
key/value: D/6
key/value: A/99
key/value: F/67
key/value: H/67
key/value: C/123
key/value: B/67
key/value: G/67
results after sorting
key/value: D/6
key/value: F/67
key/value: A/99
key/value: C/123
对于 B、G、F 和 H 键,我给出的值为 67。对地图进行排序后,它仅显示 F 值并删除 B、G 和 H 值。我想显示输出如下
key/value: D/6
key/value: B/67
key/value: G/67
key/value: F/67
key/value: H/67
key/value: A/99
key/value: C/123
I'm trying to put some key values in HashMap and then trying to sort out using TreeMap as below. Problem is that if there were similar values in map, then after sorting it is considering any one of them.
import java.util.*;
public class HashmapExample {
public static void main(String[] args) {
HashMap<String,Integer> map = new HashMap<String,Integer>();
ValueComparator bvc = new ValueComparator(map);
TreeMap<String,Integer> sorted_map = new TreeMap(bvc);
map.put("A",99);
map.put("B",67);
map.put("C",123);
map.put("G",67);
map.put("F",67);
map.put("H",67);
map.put("D",6);
System.out.println("unsorted map");
for (String key : map.keySet()) {
System.out.println("key/value: " + key + "/"+map.get(key));
}
sorted_map.putAll(map);
System.out.println("results after sorting");
for (String key : sorted_map.keySet()) {
System.out.println("key/value: " + key + "/"+sorted_map.get(key));
}
}
}
class ValueComparator implements Comparator {
Map base;
public ValueComparator(Map base) {
this.base = base;
}
public int compare(Object a,Object b) {
if((Integer)base.get(a) > (Integer)base.get(b)) {
return 1;
} else if((Integer)base.get(a) == (Integer)base.get(b)) {
return 0;
} else {
return -1;
}
}
}
After this the output is like below
unsorted map
key/value: D/6
key/value: A/99
key/value: F/67
key/value: H/67
key/value: C/123
key/value: B/67
key/value: G/67
results after sorting
key/value: D/6
key/value: F/67
key/value: A/99
key/value: C/123
For B,G,F and H keys i gave value as 67. After sorting map, it is displaying only F value and eleminating B,G and H values. I want to display outputsomething like below
key/value: D/6
key/value: B/67
key/value: G/67
key/value: F/67
key/value: H/67
key/value: A/99
key/value: C/123
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
键 B、G 和 H 被消除的原因是因为您提供的比较器仅根据值进行比较。由于它们都具有相同的值,因此它们都是相等的键,这意味着其中一个将覆盖其他键。
要打印出您想要的内容,您的比较器需要首先比较这些值,然后如果它们相等,则比较键。
The reason keys B,G and H are being eliminated is because the comparator you provided compares based only on the values. Since they all have the same values, they are all equal keys, which means one will overwrite the others.
To print out what you want, your comparator would need to first compare the values, and then if they are equal, compare the keys.
不要将 TreeSet 用于此目的或使 smt 类似
Do not use TreeSet for this purpose or make smt like
TreeSet 会删除重复项,即当compareTo() == 0 时。
我建议您让比较器在值相同时比较键,并且您应该得到。
A TreeSet removes duplicates ie when compareTo() == 0.
I suggest you have the comparator compare the keys when the values are the same and you should get.
正如大家所说,您的比较代码已损坏。用这个替换它。这不会考虑具有相同值但不同键的两对相等。
As everyone has said your compare code is broken. Replace it with this. This will not consider two pairs with the same value, but different keys, equal.
你的比较器有问题。例如,compare 方法对于 G 和 F 返回 0。因此,树状图没有与其中之一关联的键值对。
您需要研究比较器。
Your comparator has problem. e.g. compare method returns 0 for G and F. Hence the treemap doesnt have key-value pair associated with one of them.
You need to work on comparator.
根据杰夫的回答,我写了一个通用版本:
它可以按如下方式使用:
Based on Jeff's answer, I wrote a generic version:
It could be used as follows: