File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

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

-Aditya
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4461
    
    8

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

Joined: Oct 02, 2003
Posts: 11475
    
  16

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

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

Joined: Oct 27, 2005
Posts: 19759
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

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

Joined: May 03, 2008
Posts: 4658
    
    5

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: http://aspose.com/file-tools
 
subject: Get the first element of a Set