I am putting values in HashMap. But when I am reading values from that HashMap; I am NOT getting those values in the ORDER in which I have added them. Can any-one help me?.
HashMap
doesn't guarantee that the insertion-order is preserved.
Use LinkedHashMap
if you need such guarantee.
This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order).
From the Java documentation:
This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.
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