How to obtain index of a given LinkedHashSet element without iteration?
The Set
interface doesn't have something like as an indexOf()
method. You'd really need to iterate over it or to use the List
interface instead which offers an indexOf()
method.
If you would like to, converting Set
to List
is pretty trivial, it should be a matter of passing the Set
through the constructor of the List
implementation. E.g.
List<String> nameList = new ArrayList<String>(nameSet);
// ...
I don't believe so, but you could create a LinkedHashSetWithIndex
wrapper class that would do the iteration for you, or keep a separate table with the indexes of each entry, if the performance decrease is acceptable for your use case.