File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashSet ordering

 
Paul Stat
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Henry Wong
author
Marshal
Pie
Posts: 20828
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Try it with different numbers -- or with strings.

Henry
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Overriden the hashcode and equals method.
 
Henry Wong
author
Marshal
Pie
Posts: 20828
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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!
 
Vijay Raj
Ranch Hand
Posts: 110
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry, what does 'unordered' mean then? Does it mean that the order is not predictable?

Vijay.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
akash azal
Ranch Hand
Posts: 70
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey paul can you explain the question no 14 from chapter 7 of k&s
 
Henry Wong
author
Marshal
Pie
Posts: 20828
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic