Sparkplug
Sparkplug

Reputation: 495

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. Tried using TreeMap but made a mess with it. 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:

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

Upvotes: 1

Views: 305

Answers (3)

raging met
raging met

Reputation: 356

I recommend you handle the 2 lists seperately, it will look something like this

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

Upvotes: 0

Thorn
Thorn

Reputation: 4057

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

}

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 692231

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. Use objects, and encapsulate behavior in these objects.

Upvotes: 4

Related Questions