This week's book giveaway is in the NodeJS forum. We're giving away four copies of Serverless Applications with Node.js and have Slobodan Stojanovic & Aleksandar Simovic on-line! See this thread for details.
I am studying for the ocp test and in the book (Chapter 2, page 130) I found an example where the author uses a Set and HashSet interface.
I did a little research and for what I understood a HashSet is a collection of objects that will not be ordered! (if you want the objects to be ordered you should use a TreeSet).
At least this is what i understood... but for my surprise, by doing some exercises, I found out that, by using the HasSet and printing their objects, for some reason, the print statement returns the objects in an ordered way! So, I am confused
When whatever you found in your research said the the objects in a HashSet "will not be ordered", that means that there is no mechanism which causes them to be ordered. It does not mean that they must not be ordered.
So if you have a HashSet with a small number of entries, you shouldn't be surprised if they happen to be ordered. Especially if you have a HashSet with only one entry -- that entry is automatically ordered and can't be any other way! And if you have a HashSet with two entries, there's a good chance (I'm not going to say 50%) that those two entries will be ordered.
Try a HashSet with 80 entries and see what happens. I'm betting the objects won't be ordered when you display them.
posted 4 weeks ago
Thank you!! I tried what you said and as expected, the items are not ordered