This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.

the output of the above program is:
contents of HashSet are:[1,3,4,5,97,7,56,72]
can any one please explain this output to me.......
i knows that if i will use TreeSet instead of HashSet then the output will be in sorted order becoz TreeSet is Sorted collection
but in case of HashSet how this output comes after successive execution.....

This class implements the Set interface, backed by a hash table (actually a HashMap instance). It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time. This class permits the null element.

A set does not imply an order of its elements. The set {1, 2, 3} and the set {3, 2, 1} are mathematically identical. You cannot therefore expect any order from a set. A TreeSet is different, because it implements sorting in addition to the usual Set operations. A LinkedHashSet is also different, but I shall let you look it up for yourself.