• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Get the Element from Set

 
Saibabaa Pragada
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 182
C++ Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 5888
63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Think of a set as a pile of stuff" gave me what I am looking for. Thanks Stephan.
 
swaraj gupta
Ranch Hand
Posts: 182
C++ Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic