Returns a view of {@code unfiltered} containing all elements that satisfy the input predicate {@code retainIfTrue}. The returned iterable's iterator does not support {@code remove()}. <p><b>{@code Stream} equivalent:</b> {@link Stream#filter}.
(
Iterable<T> unfiltered, Predicate<? super T> retainIfTrue)
| 571 | * <p><b>{@code Stream} equivalent:</b> {@link Stream#filter}. |
| 572 | */ |
| 573 | public static <T extends @Nullable Object> Iterable<T> filter( |
| 574 | Iterable<T> unfiltered, Predicate<? super T> retainIfTrue) { |
| 575 | checkNotNull(unfiltered); |
| 576 | checkNotNull(retainIfTrue); |
| 577 | return new FluentIterable<T>() { |
| 578 | @Override |
| 579 | public Iterator<T> iterator() { |
| 580 | return Iterators.filter(unfiltered.iterator(), retainIfTrue); |
| 581 | } |
| 582 | |
| 583 | @Override |
| 584 | public void forEach(Consumer<? super T> action) { |
| 585 | checkNotNull(action); |
| 586 | unfiltered.forEach( |
| 587 | (@ParametricNullness T a) -> { |
| 588 | if (retainIfTrue.test(a)) { |
| 589 | action.accept(a); |
| 590 | } |
| 591 | }); |
| 592 | } |
| 593 | |
| 594 | @Override |
| 595 | @GwtIncompatible // Spliterator |
| 596 | public Spliterator<T> spliterator() { |
| 597 | return CollectSpliterators.filter(unfiltered.spliterator(), retainIfTrue); |
| 598 | } |
| 599 | }; |
| 600 | } |
| 601 | |
| 602 | /** |
| 603 | * Returns a view of {@code unfiltered} containing all elements that are of the type {@code |