2013-06-01 200 views
1

我有两个数组在我的哈希映射中,我想根据timeStampArray中的时间对存储在​​中的值进行排序。尝试使用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());}}   
+0

你能不能解析的数组和添加这些值,以树形图逐一?该shld排序它 – Lokesh

+0

写一个像sortLists(列表list1,列表list2)它返回一个排序的hasmap的方法,因为你的列表类型已经可比较应该是相对容易的 –

回答

4

如果我理解正确的话,你有两个平行的列表,一个包含倍,另含平均值。而且你会希望将这两个列表“并行”排序。

你最好有对象的一个​​列表,只要你想包含一个日期和一个平均值,即排序列表中的每个对象:

public final class DatedAverage { 
    private final Date date; 
    private final double average; 

    // constructor, getters omitted 
} 

... 

List<DatedAverage> datedAverages = ...; 
Collections.sort(datedAverages, new Comparator<DatedAverage>() { 
    @Override 
    public int compare(DatedAverage d1, DatedAverage d2) { 
     return d1.getDate().compareTo(d2.getDate()); 
    } 
}); 

Java是一种面向对象的语言。使用对象,并封装这些对象的行为。

0

我建议你将两个列表组合成一个对象。您可以保留您的地图,然后使用下面的静态方法使用此组合列表进行排序。

public class AverageValueTimeStamp implements Comparable<AverageValueTimeStamp>{ 
    Date timeStamp; 
    double averageValue; 

    public AverageValueTimeStamp(Date when, double avg) { 
     timeStamp = when; 
     averageValue = avg; 
    } 

    public int compareTo(AverageValueTimeStamp other) { 
     if(timeStamp.equals(other.timeStamp) 
      retrn averageValue - other.AverageValue; 
     else 
      return timeStamp.compareTo(other.timeStamp); 
    } 

    /** 
    * @return a list of AverageValueTimeStamp, sorted by Date (timestamp). 
    */ 
    public static ArrayList<AverageValueTimeStamp> toList(List<Date> timeStamps, List<Double> averages) { 
     //change to iterators if these might be LinkedLists 
     ArrayList<AverageValueTimeStamp> avtsList = new ArrayList<>(timeStamps.size()); 
     for(int i=0; i<timeStamps.size(); i++) { 
      AverageValueTimeStamp avts = new AverageValueTimeStamp(timeStamps.get(i), averages.get(i)); 
      avtsList.add(avts); 
     } 
     Collections.sort(avtsList); 
     return avtsList; 
    } 

} 
0

我建议你办理2所列出seperately,它会是这个样子

 
public HashMap sortLists(List list1, List list2){ 

     HashMap,List> map = new HashMap,List>(); 

     Collections.sort(list1); //sort the date list 

     ArrayList sorted = new ArrayList(); 

     for(Date date : list1){ 

      //your logic goes here, add objects to sorted 
      //use this method when iterating your hasmap for each key value 
        //if you want return the sorted list instead of hashmap 
     } 

     map.put(list1, sorted); 

     return map; 
    }