Sort keys which are date entries in a hashmap(排序键是哈希图中的日期条目)
问题描述
我有一个 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()));
}
这篇关于排序键是哈希图中的日期条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:排序键是哈希图中的日期条目
基础教程推荐
- 在java中使用xpath和selenium解析HTML表格数据 2022-01-01
- Java ECDSAwithSHA256 签名长度不一致 2022-01-01
- 将 double 转换为 Int,向下舍入 2022-01-01
- JPA惰性列表上的流 2022-01-01
- 在springboot中如何给mybatis加拦截器 2023-04-29
- Maven:无效的目标版本:10 2022-01-01
- 如何在相机中应用自定义滤镜 [Surfaceview 预览]. 2022-01-01
- 控制台应用程序中的 Java 键盘输入解析 2022-01-01
- 将 Windows 证书导入 Java 2022-01-01
- doFilter()是在servlet的工作完成之前还是之后执行的? 2022-01-01
