my dog learned polymorphism
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Get the Element from Set Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Get the Element from Set" Watch "Get the Element from Set" New topic
Author

Get the Element from Set

Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
Hi, How can I get a first or second element from Set..With Iterator, I can get all elements. But I want to see the specific elements..
swaraj gupta
Ranch Hand

Joined: Oct 22, 2010
Posts: 182

I think you can not because Set is an UNORDERED collection of unique elements. So, you cant expect the output in a particular order not even in the order you added the elements.
Saibabaa Pragada wrote:Hi, How can I get a first or second element from Set..With Iterator, I can get all elements. But I want to see the specific elements..
Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
Set is Unordered in general. But, Linked HashSet is in ordered..We can get the elements in the way we inserted. The given example is demonstration and you may not get order because it is HashSet. Please let me know if there is a way to get the element dynamically / or from a particular spot.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 4087
    
  18

Sets theoretically don't have a 'first' element. Think of a set as a pile of stuff. What's the first thing in a pile of stuff?

Why do you want the first element?


The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
"Think of a set as a pile of stuff" gave me what I am looking for. Thanks Stephan.
swaraj gupta
Ranch Hand

Joined: Oct 22, 2010
Posts: 182

Thanks, for letting me know about this point..
Saibabaa Pragada wrote:Set is Unordered in general. But, Linked HashSet is in ordered..We can get the elements in the way we inserted. The given example is demonstration and you may not get order because it is HashSet. Please let me know if there is a way to get the element dynamically / or from a particular spot.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Get the Element from Set
 
It's not a secret anymore!