posted 8 years ago
Abhi an unordered collection also uses some internal order. That's why it doesn't give repeat objects will iterating. Since you are just adding objects of Integer class to the map, so the internal ordering of HashMap might be ordering it in natural order. That's why you are getting sorted result. But this behavior is not guaranteed. So for HashMap or Hashtable, you will have to say that they are unordered and unsorted no matter what the output is in your case. This is just one example where you are getting sorted result. So don't rely on it. Rely on documentation which says that HashMap and Hashtable are unordered ...