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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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: 181

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: 3605
    
  14

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?
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: 181

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
 
Similar Threads
How does "natural ordering" works for strings
Re-inserting data into Set?
question about treeSet (K&B) Chapter 7 Question 3
Ordering in HashSet