Class DistinctTopK<T>
java.lang.Object
com.apple.foundationdb.async.common.DistinctTopK<T>
- Type Parameters:
T- the type of element collected
A fixed-capacity collector that retains the top
k distinct elements offered, as ordered by a
Comparator (the k that compare greatest). Duplicate elements are ignored, and once at capacity a
new element is kept only if it compares strictly greater than the current worst, which it then evicts. Backed by
a TreeSet, 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 distinct top-K as a sorted list.collectRemaining(AsyncIterator<T> iterator, Executor executor) Exhausts the iterator, offering every element to this distinct top-K collector, and returns the retained distinct top-K elements as a sorted list.static <T> DistinctTopK<T> max(Comparator<T> comparator, int k) Creates a collector that retains theklargest distinct elements offered, as ordered by the given comparator.static <T> DistinctTopK<T> min(Comparator<T> comparator, int k) Creates a collector that retains theksmallest distinct elements offered, as ordered by the given comparator.Returns the currently retained distinct elements ordered from greatest to least, as ranked by the comparator.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. An element that compares equal (by the collector'sComparator, notequals) to one already retained is ignored — so an element tying the current worst counts as a duplicate and is rejected. Otherwise the element is kept if fewer thankelements are 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 a duplicate or was rejected
-
toSortedList
Returns the currently retained distinct 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 distinct 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 distinct top-K elements, sorted from best to worst
-
collectRemaining
Exhausts the iterator, offering every element to this distinct top-K collector, and returns the retained distinct 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 distinct top-K elements, sorted
-
min
Creates a collector that retains theksmallest distinct 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 distinct elements to retain; must be positive- Returns:
- a new collector retaining the k smallest distinct elements
- Throws:
IllegalArgumentException- ifkis not positive
-
max
Creates a collector that retains theklargest distinct 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 distinct elements to retain; must be positive- Returns:
- a new collector retaining the k largest distinct elements
- Throws:
IllegalArgumentException- ifkis not positive
-