我正在尝试使用Comparator接口对TreeMap进行排序(将Double作为值并将Integer值作为键),但它不起作用。
// Create a tree map
TreeMap tm = new TreeMap();
// Put elements to the map
tm.put(1, new Double(3434.34));
tm.put(0, new Double(123.22));
tm.put(4, new Double(1378.00));
tm.put(2, new Double(99.22));
tm.put(3, new Double(-19.08));
List<Map.Entry> valueList = new ArrayList(tm.entrySet());
// Collections.sort(valueList, new Sort());
Collections.sort(valueList, new Sort());
HashMap sortedMap = new HashMap();
// Get an iterator
Iterator<Map.Entry> i = valueList.iterator();
// Display elements
while (i.hasNext()) {
Map.Entry object = i.next();
sortedMap.put(object.getKey(), object.getValue());
}
List sortedList = new ArrayList(sortedMap.entrySet());
Iterator<Map.Entry> iterator = sortedList.iterator();
while (iterator.hasNext()) {
Map.Entry entry = iterator.next();
System.out.println("Value " + entry.getValue() + "\n");
}
以下是我的Comparator类
public class Sort implements Comparator<Map.Entry> {
@Override
public int compare(Map.Entry o1, Map.Entry o2) {
// TODO Auto-generated method stub
double valueOne = (Double) o1.getValue();
double valueTwo = (Double) o2.getValue();
int returnValue =
valueOne > valueTwo ? -1 : (valueOne == valueTwo ? 0 : 1);
return (valueOne > valueTwo ? -1 : (valueOne == valueTwo ? 0 : 1));
}
}
但我得到以下输出
Value 123.22
Value 3434.34
Value 99.22
Value -19.08
Value 1378.0
Edited Part
public int compare(Map.Entry o1, Map.Entry o2) {
// TODO Auto-generated method stub
double valueOne = ((Double) o1.getValue()).doubleValue();
double valueTwo = ((Double) o2.getValue()).doubleValue();
int returnValue =
valueOne > valueTwo ? -1 : (valueOne == valueTwo ? 0 : 1);
return (valueOne > valueTwo ? -1 : (valueOne == valueTwo ? 0 : 1));
}
答案 0 :(得分:5)
HashMap
本质上是无序的。
相反,您可以首先使用比较器创建TreeMap
。
答案 1 :(得分:1)
当您将它们放入HashMap
时,它们将不会保留其订单,因为HashMap
不是有序地图。
答案 2 :(得分:1)
添加其他人已经建议的内容,试试这个:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
public class SortDemo
{
public class Sort implements Comparator<Map.Entry>
{
public int compare(Entry o1, Entry o2)
{
Double valueOne = (Double) o1.getValue();
Double valueTwo = (Double) o2.getValue();
return (int) Math.signum(valueOne.compareTo(valueTwo));
}
}
public static void main(String[] args)
{
new SortDemo().foo();
}
void foo()
{
TreeMap tm = new TreeMap();
tm.put(1, new Double(3434.34));
tm.put(0, new Double(123.22));
tm.put(4, new Double(1378.00));
tm.put(2, new Double(99.22));
tm.put(3, new Double(-19.08));
List<Map.Entry> valueList = new ArrayList(tm.entrySet());
Collections.sort(valueList, new Sort());
Iterator<Map.Entry> iterator = valueList.iterator();
while (iterator.hasNext())
{
Map.Entry entry = iterator.next();
System.out.println("Value: " + entry.getValue());
}
}
}
答案 3 :(得分:1)
Comparator
仅适用于您的密钥,而不适用于条目。您需要Comparator<Integer>
,并将此Comparator的实例传递给TreeMap
构造函数。
TreeMap<Integer,Double> tm = new TreeMap<Integer,Double>(myIntegerComparator);
在您的示例中,您看到的行为是由于TreeMap
使用Integer
的标准比较(这可行,因为Integer
是Comparable<Integer>
)。
(顺便说一下,您还应该阅读泛型并在集合类中使用它们,以及任何其他参数化类。)
答案 4 :(得分:0)
使用HashMap进行排序不起作用,因为它不是列表。您需要查看ArrayList和sort方法。
ArrayList<double> arr = new ArrayList<double>();
sort(arr) //sort ascending