Class TopK<T>
java.lang.Object
com.apple.foundationdb.async.common.TopK<T>
- Type Parameters:
T- the type of element collected
A fixed-capacity collector that retains the top
k elements offered, as ordered by a Comparator
(the k that compare greatest). Once at capacity, a new element is kept only if it compares strictly
greater than the current worst, which it then evicts. Unlike DistinctTopK this does not deduplicate, so
equal elements may be retained more than once. Backed by a bounded min-heap, giving O(log k)
add(Object).-
Method Summary
Modifier and TypeMethodDescriptionbooleanOffers an element to this collector.collect(AsyncIterable<T> iterable, Executor executor) Offers every element produced by the given iterable to this collector and returns the retained top-K as a sorted list.collectRemaining(AsyncIterator<T> iterator, Executor executor) Exhausts the iterator, offering every element to this top-K collector, and returns the retained top-K elements as a sorted list.static <T> TopK<T> max(Comparator<T> comparator, int k) Creates a collector that retains theklargest elements offered, as ordered by the given comparator.static <T> TopK<T> min(Comparator<T> comparator, int k) Creates a collector that retains theksmallest elements offered, as ordered by the given comparator.Returns the currently retained elements ordered from greatest to least, as ranked by the comparator.Returns the currently retained elements in no particular order.Returns the current worst retained element — the next one that would be evicted — or empty if none are held.
-
Method Details
-
add
Offers an element to this collector. The element is kept if fewer thankelements are currently held, or if it compares strictly greater than the current worst, which it then evicts.- Parameters:
item- the element to offer- Returns:
trueif the element was retained,falseif it was rejected
-
toUnsortedList
Returns the currently retained elements in no particular order.- Returns:
- the retained elements, unordered
-
toSortedList
Returns the currently retained elements ordered from greatest to least, as ranked by the comparator.- Returns:
- the retained elements, sorted from best (greatest) to worst (least)
-
worstElement
Returns the current worst retained element — the next one that would be evicted — or empty if none are held.- Returns:
- the worst retained element, or
Optional.empty()if the collector is empty
-
collect
Offers every element produced by the given iterable to this collector and returns the retained top-K as a sorted list.- Parameters:
iterable- the elements to offerexecutor- the executor to use for asynchronous iteration- Returns:
- a future completing with the retained top-K elements, sorted from best to worst
-
collectRemaining
Exhausts the iterator, offering every element to this top-K collector, and returns the retained top-K elements as a sorted list.- Parameters:
iterator- the source of data over which to iterate. This function will exhaust the iterator.executor- theExecutorto use for asynchronous operations- Returns:
- a
CompletableFuturecompleting with the retained top-K elements, sorted
-
min
Creates a collector that retains theksmallest elements offered, as ordered by the given comparator.- Type Parameters:
T- the type of element collected- Parameters:
comparator- the comparator defining element orderk- the maximum number of elements to retain- Returns:
- a new collector retaining the k smallest elements
-
max
Creates a collector that retains theklargest elements offered, as ordered by the given comparator.- Type Parameters:
T- the type of element collected- Parameters:
comparator- the comparator defining element orderk- the maximum number of elements to retain- Returns:
- a new collector retaining the k largest elements
-