公告
财富商城
积分规则
提问
发文
2019-01-27 21:06发布
放荡不羁爱自由
I trying to sort this HashMap based on date in keys
My Hash map:
Map<Date, ArrayList> m = new HashMap<Date, ArrayList>();
Use a TreeMap instead of HashMap. As Date already implements Comparable, it will be sorted automatically on insertion.
TreeMap
HashMap
Date
Comparable
Map<Date, ArrayList> m = new TreeMap<Date, ArrayList>();
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);
Use TreeMap instead of HashMap to store the data,it will be sorted automatically.
最多设置5个标签!
Use a
TreeMap
instead ofHashMap
. AsDate
already implementsComparable
, it will be sorted automatically on insertion.Alternatively, if you have an existing
HashMap
and want to create aTreeMap
based on it, pass it to the constructor:See also:
Use TreeMap instead of HashMap to store the data,it will be sorted automatically.