如何根据日期对HashMap进行排序? [英] How to sort HashMap based on Date?
本文介绍了如何根据日期对HashMap进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图根据键中的日期对这个HashMap进行排序
I trying to sort this HashMap based on date in keys
我的哈希表:
Map< Date,ArrayList> m = new HashMap< Date,ArrayList>();
Map<Date, ArrayList> m = new HashMap<Date, ArrayList>();
推荐答案
http://docs.oracle.com/javase/6/docs/api/java/util/TreeMap.html> TreeMap
而不是 HashMap中
。正如 Date
已经实现了 Comparable
<
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>();
或者,如果您有现有的 HashMap
并希望基于它创建一个 TreeMap
,并将其传递给构造函数:
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教程 - 地图实现
- Java教程 - 对象排序
- Java tutorials - Map implementations
- Java tutorials - Object ordering
See also:
这篇关于如何根据日期对HashMap进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文