本文介紹了排序鍵是哈希圖中的日期條目的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我有一個 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()));
}
這篇關于排序鍵是哈希圖中的日期條目的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!