Why does the java.util.Set
interface lack get(int Index)
, or any similar get()
method?
It seems that sets are great for putting things into, but I can't find an elegant way of retrieving a single item from them.
If I know I want the first item, I can use set.iterator().next()
, but otherwise it seems I have to cast to an Array to retrieve an item at a specific index?
What are the appropriate ways of retrieving data from a set? (other than using an iterator)
I'm asking this question because I had a dbUnit test, where I could reasonably assert that the returned set from a query had only 1 item, and I was trying to access that item.
So, what's the difference between Set
and List
?
Because sets have no ordering. Some implementations do (particularly those implementing the java.util.SortedSet
interface), but that is not a general property of sets.
If you're trying to use sets this way, you should consider using a list instead.