Binary search for a list, but not for a set

into Collectionthis:

binarySearch(List list, Object key)

Why is binary search not applicable for Set? Why is it just for List?

Any specific reason?

+5
source share
4 answers

Binary search involves a sorted container. The set is either disordered ( HashSet), in which case the binary search cannot be performed or ordered ( TreeSet), in which case its search operation is already effective as a binary search (i.e. O(Log2(N))).

+8
source

Binary search works on an ordered set. The set is not ordered.

+5
source

, . , .

.

, NavigableSet

+4

The set is unordered and does not contain indexes for the elements contained in it. Therefore, the binarySearch () method, which returns the index of the element, does not make sense.

+3
source

All Articles