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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With