1

我的哈希映射中有两个数组,我想对存储在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 为:

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

3 回答 3

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 是一种面向对象的语言。使用对象,并在这些对象中封装行为。

于 2013-06-01T11:52:29.887 回答
0

I recommend you combine the two lists into a single object. You can leave your map as it is and then use this combined list for the sorting using the static method below.

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

}
于 2013-06-01T12:06:35.190 回答
0

我建议您分别处理 2 个列表,它看起来像这样

public HashMap sortLists(List list1, List list2){

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

        Collections.sort(list1); //对日期列表进行排序

        ArrayList 排序 = new ArrayList();

        for(日期日期:list1){

            //你的逻辑在这里,将对象添加到排序
            //在为每个键值迭代你的hasmap时使用这个方法
                  //如果你想返回排序列表而不是hashmap
        }

        map.put(list1, 排序);

        返回地图;
    }
于 2013-06-01T13:08:38.453 回答