This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
  • 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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: 23958
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: 23958
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: 23958
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
 
straws are for suckers. tiny ads are for attractive people.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic