【问题标题】:Sort keys which are date entries in a hashmap排序键是哈希图中的日期条目
【发布时间】:2011-09-09 19:31:57
【问题描述】:

我有一个 hashMap,它具有以下值作为键 value(sql date , integer) 对:

a.put("31-05-2011",67);
a.put("01-06-2011",89);
a.put("10-06-2011",56);
a.put("25-05-2011",34);

当我尝试使用基于键对 hashMap 进行排序时: Map modified_a=new TreeMap(a); 并显示按键如下:

01-06-2011,10-06-2011,25-05-2011, 31-05-2011

但我希望将键排序为

31-05-2011,25-05-2011,01-06-2011 ,10-06-2011

我可以看到这些值是根据前 2 位数字(即日期值)进行排序的,但我还需要考虑月份值并首先根据月份进行排序,然后每个月对相应的日期进行排序. 有什么线索吗??

【问题讨论】:

    标签: java sorting hashmap


    【解决方案1】:

    您必须将自定义比较器传递给 TreeMap 构造函数,该比较器会将您的键作为日期而不是字符串进行比较(或使用 java.util.Date 作为键,在这种情况下,它将在日期实现 Comparable 时立即发生) .

    【讨论】:

      【解决方案2】:

      IMO 的最佳解决方案是为键使用不同的数据类型 - 实际上 表示日期的数据类型,并且按自然日期顺序排序。除非另有限制,否则我会使用 Joda Time 的 LocalDate 类型,它代表你想要的(只是一个日期,而不是日期/时间等)。

      如果您确实想使用字符串键但可以更改它们的格式,则可以使用 yyyy-MM-dd 格式,它自然是可排序的。

      或者,您可以将Comparator<String> 传递给TreeMap 构造函数,其中比较器是在要求比较两个字符串时解析两个字符串,并根据解析的年/月/日值。没有一个构造函数可以同时使用自定义比较器 和 一个现有的地图,所以你需要类似的东西:

      Map<String, Integer> modified = new TreeMap<String, Integer>(customComparator);
      modified.putAll(a);
      

      如果您有大量数据(由于重复解析),这种方法会相对较慢,并且编写起来有点繁琐 - 如果可以的话,我会使用更合适的数据类型。

      【讨论】:

      • +1 用于建议正确的日期类型和 Comparator 解决方案,以及说明为什么日期类型更好。
      【解决方案3】:

      您可能希望使用TreeMap 而不是HashMap,并使用提供排序的自定义Comparator 创建地图。

      这是一个匿名比较器的草稿(它不会将字符串解析为可比较的日期对象):

      new Comparator<String>() {
      
          @Override
          public int compare(String date1, String date2) {
              // skipping tests! Assuming, all date are well formatted
      
              String[] parts1 = date1.split("-");
              String[] parts2 = date2.split("-");
      
              String reordered1 = parts1[2] + parts1[1] + parts1[0];
              String reordered2 = parts2[2] + parts2[1] + parts2[0];
      
              return reordered1.compareTo(reordered2);
          }
      }
      

      【讨论】:

        【解决方案4】:

        你可以用like

        Map<Date, Integer> m = new HashMap<Date, Integer>(); 
        
            DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
        
            m.put(new java.sql.Date(dateFormat.parse("31-05-2011").getTime()),67);
            m.put(new java.sql.Date(dateFormat.parse("01-06-2011").getTime()),89);
            m.put(new java.sql.Date(dateFormat.parse("10-06-2011").getTime()),56);
            m.put(new java.sql.Date(dateFormat.parse("25-05-2011").getTime()),34);
        
        
            Map<Date, Integer> m1 = new TreeMap(m);
            DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        
            for (Map.Entry<Date, Integer> entry : m1.entrySet())
            {
                System.out.println(df.format(entry.getKey()));
            }
        

        【讨论】:

        • 谢谢,这很有帮助,但我确实有一个非常愚蠢的问题,即使在使用 SimpledateFormat 类之后,在使用您的代码并检查输出时,我发现输出是 5 月 25 日星期三 00:00:00 SGT 2011,但我希望它显示为 25/05/2011 我使用了您在上面发布的代码
        【解决方案5】:

        创建比较器:

        public class DateComparator implements Comparator<Date> {
            public int compare(Date date1, Date date2) {
                return date1.compareTo(date2);
            }
        }
        

        并将比较器与 TreeMap 一起使用

        Map<Date, Integer> comparedDates = new TreeMap<Date, Integer>(new DateComparator());
        // here fill you <Date, Integer> map like:
        comparedDates.put(new Date(System.currentTimeMillis()), 123);
        

        地图中的所有日期都会被排序。

        【讨论】:

          【解决方案6】:

          我需要对日期进行反向排序(最近的日期在前)。我使用下面的代码使它工作:

          Map<Date, Integer> dateMap = new TreeMap<Date, Integer>(new Comparator<Date>() {
              public int compare(Date date1, Date date2) {
                  return date2.compareTo(date1);
              }
          });
          

          调用dateMap.keySet() 将产生带有键的Set,其中最先返回最近的日期。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2017-07-16
            • 2011-01-08
            • 2011-05-19
            • 2012-08-17
            • 2023-03-03
            • 2021-02-18
            • 1970-01-01
            相关资源
            最近更新 更多