i need to get the keys from map in the order when they are added. but seems the map.keys return a array with re-ordered keys.
how to keep the order of the key/values as they added when retrieve them ?
Thanks
i need to get the keys from map in the order when they are added. but seems the map.keys return a array with re-ordered keys.
how to keep the order of the key/values as they added when retrieve them ?
Thanks
Maps implemented using trees don't store their keys in the order that they are inserted. That information is lost when it's inserted by value (based on its Comparable
), and further "randomized" when the tree is balanced.
As @mziccard pointed out, linear structures (such as a LinkedHashMap
) can preserve insertion order, so that's the way to go if you need such a capability.