 <p>First, your code does not have any effect. Hash table "breaks" the order. The order of elements in hash table depends on the particular hash implementation. </p> <p>There are 2 types of Maps in JDK: HashMap and SortedMap (typically we use its implementation TreeMap). BTW do not use Hashtable: this is old, synchronized and almost obsolete implementation). </p> <p>When you are using HashMap (and Hashtable) the order of keys is unpredictable: it depends on implementation of <code>hashCode()</code> method of class you are using as keys of your map. If you are using TreeMap you can use Comparator to change this logic. </p> <p>If you wish your keys to be extracted in the same order you put them use <code>LinkedHashMap</code>.</p>