I have a TreeSet<Integer> set = new TreeSet<>();
I fill the set with some Values:
set.add(i); // i=1,5,7,44,9,7
Since it is a sorted Set, I should be able to have the set as follows.
1 - 5 - 7 - 9 - 44
now I want to know the position of 9
in the set, which is 3
in this case.
1) I could do a linar search, iterating over the set, increasing a value, it the current element is not equal to the element Im searching for,
2) I could do a Binery Recursive search, determining the length, splitting the set in two, getting the last element of the first subset, determining then, in which subset I proceed the search, and so on.
Is there a cheaper method to search the index of a value in the set?