• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

can't understand the output

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.....
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is set in HashSet documentation:

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.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic