• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Get the Element from Set

 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic