• 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

HashSet ordering

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've just reached the end of chapter 7 K&B Collections & Generics, and I thought I'd practice coding some of the Collections mentioned.

It says HashSet: "an unsorted, unordered Set", if that's the case why does the following code



Produce this seemingly ordered output

1
2
3
4
5
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Try it with different numbers -- or with strings.

Henry
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Overriden the hashcode and equals method.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

BTW, "unordered" does not mean that the order is random. It does not mean that the order changes with each run. In fact, you can't infer anything here. In fact, as you have seen, for certain cases, on certain platforms, it can actually be seemingly ordered.

James Tharakan wrote:Overriden the hashcode and equals method.



While the integer class does override those methods, I don't see how that fact has any relevance to this question.

Henry
 
Paul Stat
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
Try it with different numbers -- or with strings.

Henry



Ok then, so what determines the "order" in which it's elements are printed?
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

While the integer class does override those methods, I don't see how that fact has any relevance to this question.

Henry


Yeah..... i am sorry .....
 
Paul Stat
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
BTW, "unordered" does not mean that the order is random. It does not mean that the order changes with each run. In fact, you can't infer anything here. In fact, as you have seen, for certain cases, on certain platforms, it can actually be seemingly ordered.



Hmm ok, thanks!
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Henry, what does 'unordered' mean then? Does it mean that the order is not predictable?

Vijay.
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that what Henry means (and what the documentation means by "unordered") is that you shouldn't rely on any specific order. In this case, probably due to the specific implementation, you are getting an apparently ordered sequence, but you shouldn't rely on that fact as it is not guaranteed.
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey paul can you explain the question no 14 from chapter 7 of k&s
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

akash azal wrote:hey paul can you explain the question no 14 from chapter 7 of k&s



If this is a related question, please elaborate. Otherwise, please do not hijack topics.

Henry
 
Well THAT's new! Comfort me, reliable tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic