When we output the values of a simple hashtable why it's values output in decremented order of it's key?
And how can I output in incremental order?
Hashtable<Integer, String> htable = new Hashtable<>(); htable.put(100, "Dil"); htable.put(200, "Vidu"); htable.put(300, "Apeksha"); htable.put(400, "Akalpa"); htable.put(500, "Akash"); for (Map.Entry entry : htable.entrySet()) { System.out.println(entry.getKey() + " : " + entry.getValue() + " : " + entry.hashCode()); } Output
500 : Akash : 63312920 400 : Akalpa : 1962708790 300 : Apeksha : 861238907 200 : Vidu : 2666092 100 : Dil : 68611 Also Can you please explain what actually hashcode is? Is that a random number set to make each result is unique?
Thank you.
TreeMap.LinkedHashMapas it maintains insertion order.HashtableandHashMapdoes not maintain insertion order.entry.hashCode()represents unique code for a given key-value pair. Technically, it isXORbetween key hash code and value hash code.hashCode()here: docs.oracle.com/en/java/javase/12/docs/api/java.base/java/lang/… It is inherited from theObjectclass. Note that it is not guaranteed to be unique across any set of objects, but there is a good chance that it will be.