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>

class Limiter<T> extends AbstractCollection<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 Details

    • data

      private T[] data
    • limit

      private final int limit
    • comparator

      private final Comparator<? super T> comparator
    • size

      private int size
    • initial

      private boolean initial
  • Constructor Details

    • Limiter

      public Limiter(int limit, Comparator<? super T> comparator)
  • Method Details

    • 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 other Limiter 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 calling sort() this Limiter represents the resulting collection.
    • toArray

      public Object[] toArray()
      Specified by:
      toArray in interface Collection<T>
      Overrides:
      toArray in class AbstractCollection<T>
    • iterator

      public Iterator<T> iterator()
      Specified by:
      iterator in interface Collection<T>
      Specified by:
      iterator in interface Iterable<T>
      Specified by:
      iterator in class AbstractCollection<T>
    • size

      public int size()
      Specified by:
      size in interface Collection<T>
      Specified by:
      size in class AbstractCollection<T>