List interface allows us to get the object using get() method at an index.
How can we obtain the object at the particular index in set interface like LinkedHashSet
List interface allows us to get the object using get() method at an index.
How can we obtain the object at the particular index in set interface like LinkedHashSet
Set
is unordered. There isn't the concept of index.
Therefore, if you want to get a particular element, you are forced to loop over it and break as soon as you find element you wanted.
http://docs.oracle.com/javase/7/docs/api/java/util/Set.html
and here: http://docs.oracle.com/javase/6/docs/api/java/util/LinkedHashSet.html
But a set is only used to check if something is in the list, not where it is.
Short answer is, it is not possible. However, you can get an array that has all datum from the Set you are using and then accessing it via an index. This has to do with the abstraction provided by Set which is different from List.
A Set is simply a collection that does not allow duplicates (no comments on ordering), but a List is a collection that implies ordering, so each value has an associated index.