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 <= limit < Integer.MAX_VALUE/2. Uses
O(min(limit, inputSize)) additional memory.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Comparator<? super T>
private T[]
private boolean
private final int
private int
-
Constructor Summary
Constructors -
Method Summary
Methods inherited from class java.util.AbstractCollection
add, addAll, clear, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toString
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods 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 otherLimiter
object 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:
toArray
in interfaceCollection<T>
- Overrides:
toArray
in classAbstractCollection<T>
-
iterator
- Specified by:
iterator
in interfaceCollection<T>
- Specified by:
iterator
in interfaceIterable<T>
- Specified by:
iterator
in classAbstractCollection<T>
-
size
public int size()- Specified by:
size
in interfaceCollection<T>
- Specified by:
size
in classAbstractCollection<T>
-