简体   繁体   English

对HashMaps进行排序

[英]Sorting HashMaps

I have two arrays in my Hash map and I want to sort the values stored in the averageValueArray according to the time in the timeStampArray . 我的Hash映射中有两个数组,我想根据averageValueArray中的时间对timeStampArray存储的值进行排序。 Tried using TreeMap but made a mess with it. 尝试使用TreeMap,但弄得一团糟。 Any help will be deeply appreciable. 任何帮助都会非常明显。

Map<List<Date>,List<Double>> unsortedMap = new HashMap<List<Date>,List<Double>>();
            unsortedMap.put(timeStampArray, averageValueArray);

This is what I am trying 这就是我想要的

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

And printMap as: 而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());}}          

If I understand correctly, you have two parallel lists, one containing times, and the other containing averages. 如果我理解正确,你有两个并行列表,一个包含时间,另一个包含平均值。 And you would want the two lists to be sorted "in parallel". 你希望这两个列表“并行”排序。

You'd better have a single list of objects, each object containing a date and an average, and sort that list as you want: 您最好有一个对象列表,每个对象包含一个日期和一个平均值,并根据需要对该列表进行排序:

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 is an OO language. Java是一种OO语言。 Use objects, and encapsulate behavior in these objects. 使用对象,并在这些对象中封装行为。

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

}

I recommend you handle the 2 lists seperately, it will look something like this 我建议你单独处理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