• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Get the first element of a Set

 
Aditya Sirohi
Ranch Hand
Posts: 93
Eclipse IDE Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Friends,

I have a silly question i guess. I want to get the first element from a Set. If i use iterator i parse through the whole Set. The other way is to put the elements of the Set into ArrayList and then do .get(0) on the ArrayList.
Is there any other more efficient way to do this that is not coming in my mind right now?

Help is appreciated

-Aditya
 
Matthew Brown
Bartender
Posts: 4567
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd suggest getting an Iterator and then just call next() once. You don't need to iterate through the whole lot.

The reason the Set interface doesn't give you an obvious way of doing this is because Sets are unordered and therefore, strictly speaking, talking about the "first element of a Set" doesn't make sense.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12149
31
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct me if I am wrong, but a Set (at least in the mathematical definition) does not have an order, therefore, there is no 'first' element.

The API for the Set interface says the iterator will return the elements "in no particular order (unless this set is an instance of some class that provides a guarantee)."

 
Daniel Marti
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Set has no order, so there is not "first" or "last" item in a Set.
However...
It is impossible at the moment to get the value at a specific index in a Set (see top answers to why).
You can however save a bit in processing by, instead of introducing the set in an Arraylist, you turn it into an Array and get the position [0].
Lets take a HashSet as example (using strings as content):



I would like to warn you that calling this will NOT guarantee that the String in myStringAtIndex will be the first String introduced.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49472
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most likely you will get the String whose hash code has the the lowest remainder when divided by 16.
 
Rob Spoor
Sheriff
Pie
Posts: 20559
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd prefer Matthew's solution of using iterator().next(). Both creating an ArrayList and calling toArray will loop through the entire Set, whereas iterator().next() really inspects only one single element.
 
Stephan van Hulst
Bartender
Pie
Posts: 5912
66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even better would be to use the proper level of abstraction. If you require the "first" element, you need either a List or a SortedSet, so your method should require a List or a SortedSet.
 
Darryl Burke
Bartender
Posts: 5136
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if you really need to maintain insertion order, use a LinkedHashSet.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic