【发布时间】:2013-06-01 11:41:28
【问题描述】:
我的哈希映射中有两个数组,我想根据timeStampArray 中的时间对存储在averageValueArray 中的值进行排序。尝试使用 TreeMap 但弄得一团糟。
任何帮助都将不胜感激。
Map<List<Date>,List<Double>> unsortedMap = new HashMap<List<Date>,List<Double>>();
unsortedMap.put(timeStampArray, averageValueArray);
这就是我正在尝试的
Map<List<Date>,List<Double>> sortMap = new HashMap<List<Date>,List<Double>>();
sortMap.put(timeStampArray, averageValueArray);
for (Map.Entry entry : sortMap.entrySet()) {
System.out.println("Key = " + entry.getKey());
System.out.println(" Value = " +entry.getValue());
}
System.out.println("Unsort Map......");
printMap(sortMap);
System.out.println("Sorted Map......");
Map<List<Date>,List<Double>> treeMap = new TreeMap<List<Date>,List<Double>>(sortMap);
printMap(treeMap);
而 printMap 为:
public static void printMap(Map<List<Date>,List<Double>> map) {
for (Map.Entry entry : map.entrySet()) {
System.out.println("Key : " + entry.getKey() + " Value : "
+ entry.getValue());}}
【问题讨论】:
-
你不能解析数组并将这些值一一添加到树形图中吗?对它进行排序
-
编写一个类似 sortLists(List list1, List list2) 的方法,它返回一个排序的 hasmap,并且由于您的列表类型已经具有可比性,应该相对容易
标签: java hashmap mapping treemap