将散列键中的日期条目排序 [英] Sort keys which are date entries in a hashmap

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

问题描述

我有一个hashMap,它有以下值作为键值(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

但我希望按键排序为 p>

but I want the keys to be sorted as

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

我可以看到这些值是根据前两位数字(这是日期值)排序的,但我需要月份值也要先考虑和排序,然后每月对相应的日期进行排序。
任何线索??

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 ??

推荐答案

您可以像

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天全站免登陆