XCollectors { public static <T, A, R> Collector<T, ?, R> filtering(final Predicate<? super T> predicate, final Collector<? super T, A, R> downstream) { BiConsumer<A, ? super T> downstreamAccumulator = downstream.accumulator(); return new CollectorImpl<>(downstream.supplier(), (r, t) -> { if (predicate.test(t)) { downstreamAccumulator.accept(r, t); } }, downstream.combiner(), downstream.finisher(), downstream.characteristics()); } private  XCollectors(); static Collector<T, ?, ArrayDeque<T>> last(final int limit); static Collector<T, ArrayDeque<T>,
         ArrayDeque<T>> last(final int limit, final X addIfLimited); static Collector<T, D, D> last(final Supplier<D> dqSupp,
          final int limit, final X addIfLimited); static void limitDequeue(final Deque<T> l1, final int limit, final T addIfLimited); static Collector<T, ?, R> filtering(final Predicate<? super T> predicate,
                  final Collector<? super T, A, R> downstream);  }
@Test public void testFiltering() { ArrayDeque<Integer> collect = java.util.Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 0).stream() .collect(XCollectors.filtering((n) -> n <= 5, XCollectors.last(3, -1))); Assert.assertThat(collect, Matchers.contains(-1, 5, 0)); Assert.assertEquals(3, collect.size()); }