本文介紹了如何根據(jù)日期對(duì) HashMap 進(jìn)行排序?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我試圖根據(jù)鍵中的日期對(duì)這個(gè) HashMap 進(jìn)行排序
I trying to sort this HashMap based on date in keys
我的哈希圖:
Map
推薦答案
使用 TreeMap
而不是 哈希映射
.由于 Date
已經(jīng)實(shí)現(xiàn)了 Comparable
,插入時(shí)會(huì)自動(dòng)排序.
Use a TreeMap
instead of HashMap
. As Date
already implements Comparable
, it will be sorted automatically on insertion.
Map<Date, ArrayList> m = new TreeMap<Date, ArrayList>();
或者,如果您有一個(gè)現(xiàn)有的 HashMap
并希望基于它創(chuàng)建一個(gè) TreeMap
,請(qǐng)將其傳遞給構(gòu)造函數(shù):
Alternatively, if you have an existing HashMap
and want to create a TreeMap
based on it, pass it to the constructor:
Map<Date, ArrayList> sortedMap = new TreeMap<Date, ArrayList>(m);
另見:
- Java 教程 - 地圖實(shí)現(xiàn)
- Java 教程 - 對(duì)象排序
這篇關(guān)于如何根據(jù)日期對(duì) HashMap 進(jìn)行排序?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!