File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes HashSet ordering Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "HashSet ordering" Watch "HashSet ordering" New topic
Author

HashSet ordering

Paul Stat
Ranch Hand

Joined: Jan 20, 2009
Posts: 50
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
Sheriff

Joined: Sep 28, 2004
Posts: 18113
    
  39


Try it with different numbers -- or with strings.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Overriden the hashcode and equals method.


SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18113
    
  39


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

Joined: Jan 20, 2009
Posts: 50
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

Joined: Aug 29, 2008
Posts: 580

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

Joined: Jan 20, 2009
Posts: 50
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

Joined: Oct 10, 2005
Posts: 110
Henry, what does 'unordered' mean then? Does it mean that the order is not predictable?

Vijay.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
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.


All code in my posts, unless a source is explicitly mentioned, is my own.
akash azal
Ranch Hand

Joined: Jan 31, 2009
Posts: 70
hey paul can you explain the question no 14 from chapter 7 of k&s


We will keep things moving!!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18113
    
  39

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HashSet ordering
 
Similar Threads
Generics -really very hard question
Finish my code please
Why am able to pass a Set (non-generic) to a method expecting Set<Number>?
How to work with sets?
Why this block of code behave differently..?