Package one.util.streamex
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 java.util.AbstractCollection
add, addAll, clear, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toStringMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.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
Merge otherLimiterobject into this (other object becomes unusable after that).- Parameters:
ls- other object to merge- Returns:
- this object
-
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
- Specified by:
iteratorin interfaceCollection<T>- Specified by:
iteratorin interfaceIterable<T>- Specified by:
iteratorin classAbstractCollection<T>
-
size
public int size()- Specified by:
sizein interfaceCollection<T>- Specified by:
sizein classAbstractCollection<T>
-