I have a HashMap and I need sort him by key. What must I do? If you will give me example than I will be glad. Thank you for, anyway.
2 Answers
Pick an implementation of SortedMap (for example TreeMap), and :
- If your keys implement
Comparableuse use the copy-constructor (new TreeMap(existingMap)) to put your existing map elements to the sorted map. - If your keys are not comparable, create the
TreeMapwith aComparatorand then usetreeMap.addAll(existingMap)