Venkat Papana Venkat Papana - 2 months ago 8
Java Question

How to iterate a HashMap using the natural entrySet() order?

My Map contains keys sorted in alphabetical order. When I display it, I'm using entrySet().iterator(), but my results are not in the alphabetical order.
How can I get my results in order?

kan kan
Answer

Use TreeMap:

A Red-Black tree based NavigableMap implementation. The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used...

Comments