2013-06-01 3 views
1

해시 맵에 두 개의 배열이 있는데 timeStampArray의 시간에 따라 averageValueArray에 저장된 값을 정렬하려고합니다. TreeMap을 사용해 보았지만 엉망으로 만들었습니다. 도움이 될 것입니다. HashMaps 정렬

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

당신이 배열을 분석하고 하나 하나 트리 맵에 그 값을 추가 할 수 있습니까? that shld sort – Lokesh

+0

정렬 된 hasmap을 반환하는 sortLists (List list1, List list2)와 같은 메소드를 작성하고 목록 유형이 이미 비교 가능하기 때문에 비교적 쉽다. –

답변

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()); 
    } 
}); 

자바는 객체 지향 언어입니다. 객체를 사용하고 이러한 객체에 동작을 캡슐화합니다.

0

두 목록을 단일 개체로 결합하는 것이 좋습니다. 지도를있는 그대로 그대로두고이 결합 된 목록을 아래의 정적 방법을 사용하여 정렬에 사용할 수 있습니다. 난 당신이 별도로 2 개 목록을 처리하는 것이 좋습니다

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

, 그것은 다음과 같이 보일 것입니다

 
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; 
    }