java - Same iteration order on Map.keySet and Map.values? - Stack Overflow
No, there is no guarantee, although in practice it will happen (there's no good reason for the map to use a different iterator for the keys and values).
If you want to guarantee iteration order, iterate the entrySet():
for (Map.Entry<Integer,Integer> entry : map.entrySet()) // ... Since you ask about HashMap, note also that any changes to the map will potentially change iteration order, as a result of the mapbeing rehashed.
Read full article from java - Same iteration order on Map.keySet and Map.values? - Stack Overflow
No comments:
Post a Comment