簡體   English   中英

排序鍵是 hashmap 中的日期條目

[英]Sort keys which are date entries in a hashmap

我有一個 hashMap ,它具有以下值作為鍵值value(sql date, integer)對:

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); 並顯示按鍵如下:

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 位數字(即日期值)進行排序的,但我還需要考慮月份值並首先根據月份進行排序,然后每個月對相應的日期進行排序。 有什么線索嗎??

最好的解決方案 IMO 將使用不同的數據類型作為鍵 - 一種實際表示日期的數據類型,並按自然日期順序排序。 除非另有限制,否則我會使用Joda TimeLocalDate類型,它完全代表您想要的(只是一個日期,而不是日期/時間等)。

如果您確實想使用字符串鍵但可以更改它們的格式,則可以使用 yyyy-MM-dd 格式,它自然是可排序的。

或者,您可以Comparator<String>傳遞給TreeMap構造函數,其中比較器是在要求比較兩個字符串時解析兩個字符串,並根據解析的年/月/日值執行比較的比較器。 沒有一個構造函數可以同時使用自定義比較器現有的 map,所以你需要類似的東西:

Map<String, Integer> modified = new TreeMap<String, Integer>(customComparator);
modified.putAll(a);

如果您有大量數據(由於重復解析),這種方法將相對較慢,並且編寫起來有點繁瑣 - 如果可能的話,我會使用更合適的數據類型。

你可以使用喜歡

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

我需要對日期進行反向排序(首先是最近的日期)。 我使用下面的代碼使它工作:

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

您必須將自定義比較器傳遞給 TreeMap 構造函數,它將您的鍵作為日期而不是字符串進行比較(或使用 java.util.Date 作為鍵,在這種情況下,它將在日期實現 Comparable 時立即發生)。

創建比較器:

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);

您 Map 中的所有日期都將被排序。

您可能希望使用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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM