k elements added to it, relative to a provided
comparator. "Top" can mean the greatest or the lowest elements, specified in the factory used to
create the TopKSelector instance.
If your input data is available as a Stream, prefer passing Comparators#least(int) to Stream#collect(java.. If it is available
as an Iterable or Iterator, prefer Ordering#leastOf(Iterable, int).
This uses the same efficient implementation as Ordering#leastOf(Iterable, int),
offering expected O(n + k log k) performance (worst case O(n log k)) for n calls to offer and a call to topK, with O(k) memory. In comparison, quickselect has the same
asymptotics but requires O(n) memory, and a PriorityQueue implementation takes O(n log
k). In benchmarks, this implementation performs at least as well as either implementation, and
degrades more gracefully for worst-case input.
The implementation does not necessarily use a stable sorting algorithm; when multiple equivalent elements are added to it, it is undefined which will come first in the output.
| Modifier and Type | Field and Description |
|---|---|
| private final @Nullable T[] | |
| private int | |
| private final Comparator | |
| private final int | |
| private T | threshold
The largest of the lowest k elements we've seen so far relative to this comparator. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv TopKSelector | |
| public static <T extends Comparable | |
| public static <T extends @Nullable Object> TopKSelector | greatest(int k, Comparator<? super T> comparator)
Returns a |
| public static <T extends Comparable | |
| public static <T extends @Nullable Object> TopKSelector | least(int k, Comparator<? super T> comparator)
Returns a |
| public void | |
| public void | |
| public void | |
| private int | partition(int left, int right, int pivotIndex)
Partitions the contents of buffer in the range [left, right] around the pivot element previously stored in buffer[pivotValue]. |
| private void | |
| public List | topK()
Returns the top |
| private void |
| buffer | back to summary |
|---|---|
| private final @Nullable T[] buffer | |
| bufferSize | back to summary |
|---|---|
| private int bufferSize | |
| comparator | back to summary |
|---|---|
| private final Comparator<? super T> comparator | |
| k | back to summary |
|---|---|
| private final int k | |
| threshold | back to summary |
|---|---|
| private T threshold The largest of the lowest k elements we've seen so far relative to this comparator. If bufferSize ≥ k, then we can ignore any elements greater than this value. | |
| TopKSelector | back to summary |
|---|---|
| private TopKSelector(Comparator<? super T> comparator, int k) | |
| combine | back to summary |
|---|---|
| pack-priv TopKSelector | |
| greatest | back to summary |
|---|---|
| public static <T extends Comparable Returns a
| |
| greatest | back to summary |
|---|---|
| public static <T extends @Nullable Object> TopKSelector Returns a
| |
| least | back to summary |
|---|---|
| public static <T extends Comparable Returns a
| |
| least | back to summary |
|---|---|
| public static <T extends @Nullable Object> TopKSelector Returns a
| |
| offer | back to summary |
|---|---|
| public void offer(T elem) Adds | |
| offerAll | back to summary |
|---|---|
| public void offerAll(Iterable<? extends T> elements) Adds each member of If all input data to this | |
| offerAll | back to summary |
|---|---|
| public void offerAll(Iterator<? extends T> elements) Adds each member of If all input data to this | |
| partition | back to summary |
|---|---|
| private int partition(int left, int right, int pivotIndex) Partitions the contents of buffer in the range [left, right] around the pivot element previously stored in buffer[pivotValue]. Returns the new index of the pivot element, pivotNewIndex, so that everything in [left, pivotNewIndex] is ≤ pivotValue and everything in (pivotNewIndex, right] is greater than pivotValue. | |
| swap | back to summary |
|---|---|
| private void swap(int i, int j) | |
| topK | back to summary |
|---|---|
| public List Returns the top The returned list is an unmodifiable copy and will not be affected by further changes to
this | |
| trim | back to summary |
|---|---|
| private void trim() Quickselects the top k elements from the 2k elements in the buffer. O(k) expected time, O(k log k) worst case. | |