【问题标题】:Sorting HashMaps对 HashMap 进行排序
【发布时间】:2013-06-01 11:41:28
【问题描述】:

我的哈希映射中有两个数组,我想根据timeStampArray 中的时间对存储在averageValueArray 中的值进行排序。尝试使用 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());}}          

【问题讨论】:

  • 你不能解析数组并将这些值一一添加到树形图中吗?对它进行排序
  • 编写一个类似 sortLists(List list1, List list2) 的方法,它返回一个排序的 hasmap,并且由于您的列表类型已经具有可比性,应该相对容易

标签: java hashmap mapping treemap


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    我建议您将两个列表组合成一个对象。您可以保持地图不变,然后使用下面的静态方法使用此组合列表进行排序。

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

    【讨论】:

      【解决方案3】:

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

      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, 排序); 返回地图; }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-12-04
        • 2011-09-20
        • 2017-09-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多