Is it even possible?
Say you have
private Set<String> names = new LinkedHashSet<String>();
and Strings are “Mike”, “John”, “Karen”.
Is it possible to get “1” in return to “what’s the index of “John” without iteration?
The following works fine .. with this question i wonder if there is a better way
for (String s : names) {
++i;
if (s.equals(someRandomInputString)) {
break;
}
}
The
Setinterface doesn’t have something like as anindexOf()method. You’d really need to iterate over it or to use theListinterface instead which offers anindexOf()method.If you would like to, converting
SettoListis pretty trivial, it should be a matter of passing theSetthrough the constructor of theListimplementation. E.g.