This question is just out of curiosity, but I've noticed that iterating over a HashMap using keyset seems to always go in sorted order. Obviously insertion order is never preserved with HashMaps, but sorted key order seems a bit strange to me. Using this example:
import java.util.HashMap;
public class test {
public static void main(String[] args) {
HashMap<Integer, String> someMap = new HashMap<Integer, String>();
someMap.put(0, "nothing");
someMap.put(7, "nothing");
someMap.put(3, "nothing");
someMap.put(4, "nothing");
someMap.put(10, "nothing");
someMap.put(11, "nothing");
someMap.put(2, "nothing");
someMap.put(1, "nothing");
someMap.put(5, "nothing");
for (Integer someInt : someMap.keySet()) {
System.out.println(someInt);
}
}
}
output:
0
1
2
3
4
5
7
10
11
Is this dependable and consistent? If the HashMap implementation is an array, this would make sense if keyset just iterates over all available array indexes starting from 0, but I'm probably wrong and it's more complex than that.