Class Limiter<T>
java.lang.Object
java.util.AbstractCollection<T>
one.util.streamex.Limiter<T>
- Type Parameters:
T- type of input elements
- All Implemented Interfaces:
Iterable<T>, Collection<T>
Extracts least limit elements from the input sorting them according to the
given comparator. Works for 2 invalid input: '<'= limit invalid input: '<' Integer.MAX_VALUE/2. Uses
O(min(limit, inputSize)) additional memory.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Comparator<? super T> private T[]private booleanprivate final intprivate int -
Constructor Summary
Constructors -
Method Summary
Methods inherited from class AbstractCollection
add, addAll, clear, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toStringMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface Collection
equals, hashCode, parallelStream, removeIf, spliterator, stream, toArray
-
Field Details
-
data
-
limit
private final int limit -
comparator
-
size
private int size -
initial
private boolean initial
-
-
Constructor Details
-
Limiter
-
-
Method Details
-
put
Accumulate new element- Parameters:
t- element to accumulate- Returns:
- false if the element is definitely not included into result, so any bigger element could be skipped as well, or true if element will probably be included into result.
-
putAll
-
sortTail
private void sortTail() -
sort
public void sort()Must be called after accumulation is finished. After callingsort()this Limiter represents the resulting collection. -
toArray
- Specified by:
toArrayin interfaceCollection<T>- Overrides:
toArrayin classAbstractCollection<T>
-
iterator
-
size
public int size()- Specified by:
sizein interfaceCollection<T>- Specified by:
sizein classAbstractCollection<T>
-