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.
Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Set and HashSet  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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  

This is my code:



Hope you guys can help me on this topic!

 
Sheriff
Posts: 24295
55
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mario, welcome to the Ranch!

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.
 
Mario Segovia
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you!! I tried what you said and as expected, the items are not ordered
 
Marshal
Posts: 63843
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mario Segovia wrote: . . . Set and HashSet interface. . . .

Please be accurate with your words; Set is an interface, but HashSet is a class.
 
A wop bop a lu bop a womp bam boom! Tiny ad:
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!