File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Get the first element of a Set Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Get the first element of a Set" Watch "Get the first element of a Set" New topic

Get the first element of a Set

Aditya Sirohi
Ranch Hand

Joined: Jan 05, 2010
Posts: 93

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

Matthew Brown

Joined: Apr 06, 2010
Posts: 4543

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

Joined: Oct 02, 2003
Posts: 11955

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)."

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Daniel Marti
Ranch Hand

Joined: Jun 08, 2011
Posts: 37
A Set has no order, so there is not "first" or "last" item in a Set.
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

Joined: Oct 13, 2005
Posts: 46367
Most likely you will get the String whose hash code has the the lowest remainder when divided by 16.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20274

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.

How To Ask Questions How To Answer Questions
Stephan van Hulst

Joined: Sep 20, 2010
Posts: 4625

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.

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.
Darryl Burke

Joined: May 03, 2008
Posts: 5115

And if you really need to maintain insertion order, use a LinkedHashSet.

luck, db
There are no new questions, but there may be new answers.
I agree. Here's the link:
subject: Get the first element of a Set
It's not a secret anymore!