对HashMaps进行排序

时间:2013-06-01 11:41:28

标签: java hashmap mapping treemap

我的Hash地图中有两个数组,我想根据averageValueArray中的时间对存储在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 as:

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

3 个答案:

答案 0 :(得分: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是一种OO语言。使用对象,并在这些对象中封装行为。

答案 1 :(得分: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;
    }

}

答案 2 :(得分:0)

我建议你单独处理2个列表,它看起来像这样

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