如何使用 -ve 值作为键对树图进行排序
How to sort treemap with -ve values as key
我有一个地图,其中包含 +ve 和 -ve 值作为键
Map<String,Integer> dr=new TreeMap<String,Integer>();
所以我在输入负值时输入了“-1”。因为这是一个树状图,所以值以排序的方式存储。在我输入值并使用此函数打印树状图后
for (Map.Entry<String, Integer> entry : dr.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
输出
-1: 4
-2: 3
-3: 5
0: 1
1: 2
2: 5
3: 6
理想情况下,这应该是输出吗?
-3: 5
-2: 3
-1: 4
0: 1
1: 2
2: 5
3: 6
我怎样才能达到这个输出?
TreeMap sorts using the key. In this case, your key should be Integer, Use Map instead
Map<Integer,Integer> dr=new TreeMap<Integer,Integer>();
baam,答案来了。 :)
我有一个地图,其中包含 +ve 和 -ve 值作为键
Map<String,Integer> dr=new TreeMap<String,Integer>();
所以我在输入负值时输入了“-1”。因为这是一个树状图,所以值以排序的方式存储。在我输入值并使用此函数打印树状图后
for (Map.Entry<String, Integer> entry : dr.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
输出
-1: 4
-2: 3
-3: 5
0: 1
1: 2
2: 5
3: 6
理想情况下,这应该是输出吗?
-3: 5
-2: 3
-1: 4
0: 1
1: 2
2: 5
3: 6
我怎样才能达到这个输出?
TreeMap sorts using the key. In this case, your key should be Integer, Use Map instead
Map<Integer,Integer> dr=new TreeMap<Integer,Integer>();
baam,答案来了。 :)