排序键是哈希图中的日期条目 [英] Sort keys which are date entries in a hashmap

查看:29
本文介绍了排序键是哈希图中的日期条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a hashMap which has the following values as key value(sql date , integer) pairs:

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);并显示按键如下:

when i try to sort the hashMap based on the keys using : Map modified_a=new TreeMap(a); and display the keys it is as follows :

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 位数字(即日期值)进行排序的,但我还需要考虑月份值并首先根据月份进行排序,然后每个月对相应的日期进行排序.有什么线索吗??

I can see that the values are being sorted based on the first 2 digits( which is the date value) but I need the month value to also be considered and sort based on months first and then for each month sort the corresponding days. Any clues ??

推荐答案

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

这篇关于排序键是哈希图中的日期条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆