在哈希表中对日期条目进行排序键

8

我有一个 hashMap,它的键值对如下:value(sql日期, 整数)

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

当我尝试使用以下代码根据key对hashMap进行排序时: Map modified_a=new TreeMap(a); 并且显示keys,结果如下:

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

但我希望按照键排序
31-05-2011,25-05-2011,01-06-2011 ,10-06-2011

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

6个回答

10
你可以使用像这样的方式
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类,在使用您的代码并检查输出后,我看到输出是Wed May 25 00:00:00 SGT 2011,但我想要显示为25/05/2011。我已经使用了您上面发布的代码。 - bhavya

9
在我看来,最好的解决方案是使用不同的数据类型作为键——一种实际表示日期并按自然日期顺序排序的数据类型。除非有其他限制,我会使用Joda TimeLocalDate类型,它正好表示你想要的内容(只是日期,而不是日期/时间等)。
如果你真的想使用字符串键但可以更改其格式,可以使用yyyy-MM-dd格式,这是自然可排序的。
或者,您可以将一个Comparator<String>传递给TreeMap构造函数,其中比较器在被要求比较两个字符串时解析它们,并根据解析的年/月/日值执行比较。虽然没有同时接受自定义比较器和现有映射的构造函数,但您需要像下面这样的内容:
Map<String, Integer> modified = new TreeMap<String, Integer>(customComparator);
modified.putAll(a);

如果你有很多数据(由于重复解析),这种方法会比较慢,而且稍微有些棘手 - 如果可能的话,我建议使用更适合的数据类型。


+1 建议使用适当的日期类型和比较器解决方案,并指出为什么该日期类型更好。 - andrewdski

8

我有一个要求,需要对日期进行倒序排序(最近的日期排在第一位)。我通过以下代码实现了这个功能:

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,其中包含键,最近的日期将首先返回。


2
你需要在TreeMap构造函数中传递一个自定义比较器,以按日期而不是字符串比较你的键(或使用java.util.Date作为键,在这种情况下它将默认按日期排序,因为日期实现了Comparable接口)。请勿添加解释,保留HTML标签。

2
创建比较器:
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);

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

0
你可能想使用 TreeMap 而不是 HashMap,并创建一个带有自定义 Comparator 的 Map 来提供排序。
以下是匿名比较器的草稿(它不会将字符串解析为可比较的日期对象):
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);
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接