繁体   English   中英

对HashMaps进行排序

[英]Sorting HashMaps

我的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为:

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

如果我理解正确,你有两个并行列表,一个包含时间,另一个包含平均值。 你希望这两个列表“并行”排序。

您最好有一个对象列表,每个对象包含一个日期和一个平均值,并根据需要对该列表进行排序:

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

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

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个列表,它看起来像这样

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM