Is there an equivalent in Java for Python's bisect module? With Python's bisect you can do array bisection with directions. For instance bisect.bisect_left
does:
Locate the proper insertion point for item in list to maintain sorted order. The parameters lo and hi may be used to specify a subset of the list which should be considered; by default the entire list is used.
I know I can do this manually with a binary search too, but I was wondering if there is already a library or collection doing this.
You have two options:
java.util.Arrays.binarySearch
on arrays
java.util.Collections.binarySearch
on List
Comparable
and Comparator
overloads).List.subList(int fromIndex, int toIndex)
to search portion of a list