@GwtCompatible @Beta final class SortedLists extends java.lang.Object
List
instances.
In this documentation, the terms greatest, greater, least, and
lesser are considered to refer to the comparator on the elements, and the terms
first and last are considered to refer to the elements' ordering in a
list.Modifier and Type | Class and Description |
---|---|
static class |
SortedLists.KeyAbsentBehavior
A specification for which index to return if the list contains no elements that compare as
equal to the key.
|
static class |
SortedLists.KeyPresentBehavior
A specification for which index to return if the list contains at least one element that
compares as equal to the key.
|
Modifier | Constructor and Description |
---|---|
private |
SortedLists() |
Modifier and Type | Method and Description |
---|---|
static <E> int |
binarySearch(java.util.List<? extends E> list,
E key,
java.util.Comparator<? super E> comparator,
SortedLists.KeyPresentBehavior presentBehavior,
SortedLists.KeyAbsentBehavior absentBehavior)
Searches the specified list for the specified object using the binary search algorithm.
|
static <E extends java.lang.Comparable> |
binarySearch(java.util.List<? extends E> list,
E e,
SortedLists.KeyPresentBehavior presentBehavior,
SortedLists.KeyAbsentBehavior absentBehavior)
Searches the specified naturally ordered list for the specified object using the binary search
algorithm.
|
static <E,K> int |
binarySearch(java.util.List<E> list,
Function<? super E,K> keyFunction,
K key,
java.util.Comparator<? super K> keyComparator,
SortedLists.KeyPresentBehavior presentBehavior,
SortedLists.KeyAbsentBehavior absentBehavior)
Binary searches the list for the specified key, using the specified key function.
|
static <E,K extends java.lang.Comparable> |
binarySearch(java.util.List<E> list,
Function<? super E,K> keyFunction,
K key,
SortedLists.KeyPresentBehavior presentBehavior,
SortedLists.KeyAbsentBehavior absentBehavior)
Binary searches the list for the specified key, using the specified key function.
|
public static <E extends java.lang.Comparable> int binarySearch(java.util.List<? extends E> list, E e, SortedLists.KeyPresentBehavior presentBehavior, SortedLists.KeyAbsentBehavior absentBehavior)
Equivalent to binarySearch(List, Function, Object, Comparator, KeyPresentBehavior,
KeyAbsentBehavior)
using Ordering.natural()
.
public static <E,K extends java.lang.Comparable> int binarySearch(java.util.List<E> list, Function<? super E,K> keyFunction, @Nullable K key, SortedLists.KeyPresentBehavior presentBehavior, SortedLists.KeyAbsentBehavior absentBehavior)
Equivalent to binarySearch(List, Function, Object, Comparator, KeyPresentBehavior,
KeyAbsentBehavior)
using Ordering.natural()
.
public static <E,K> int binarySearch(java.util.List<E> list, Function<? super E,K> keyFunction, @Nullable K key, java.util.Comparator<? super K> keyComparator, SortedLists.KeyPresentBehavior presentBehavior, SortedLists.KeyAbsentBehavior absentBehavior)
Equivalent to
binarySearch(List, Object, Comparator, KeyPresentBehavior, KeyAbsentBehavior)
using
Lists.transform(list, keyFunction)
.
public static <E> int binarySearch(java.util.List<? extends E> list, @Nullable E key, java.util.Comparator<? super E> comparator, SortedLists.KeyPresentBehavior presentBehavior, SortedLists.KeyAbsentBehavior absentBehavior)
Collections.sort(List, Comparator)
method), prior
to making this call. If it is not sorted, the results are undefined.
If there are elements in the list which compare as equal to the key, the choice of
SortedLists.KeyPresentBehavior
decides which index is returned. If no elements compare as equal to
the key, the choice of SortedLists.KeyAbsentBehavior
decides which index is returned.
This method runs in log(n) time on random-access lists, which offer near-constant-time access to each list element.
list
- the list to be searched.key
- the value to be searched for.comparator
- the comparator by which the list is ordered.presentBehavior
- the specification for what to do if at least one element of the list
compares as equal to the key.absentBehavior
- the specification for what to do if no elements of the list compare as
equal to the key.KeyPresentBehavior
, if the key is in the list;
otherwise the index determined by the KeyAbsentBehavior
.