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:
java.lang.Iterable<T>
,java.util.Collection<T>
class Limiter<T> extends java.util.AbstractCollection<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.
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.util.Iterator<T>
iterator()
boolean
put(T t)
Accumulate new elementLimiter<T>
putAll(Limiter<T> ls)
Merge otherLimiter
object into this (other object becomes unusable after that).int
size()
void
sort()
Must be called after accumulation is finished.private void
sortTail()
java.lang.Object[]
toArray()
-
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
-
-
-
-
Constructor Detail
-
Limiter
public Limiter(int limit, java.util.Comparator<? super T> comparator)
-
-
Method Detail
-
put
public boolean put(T t)
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
public Limiter<T> putAll(Limiter<T> ls)
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
public java.lang.Object[] toArray()
-
iterator
public java.util.Iterator<T> iterator()
-
-