Returns a view of the elements of unfiltered that satisfy a predicate. The returned multiset is a live view of unfiltered; changes to one affect the other. The resulting multiset's iterators, and those of its entrySet() and elementSet(), do not support {@code remo
(Multiset<E> unfiltered, Predicate<? super E> predicate)
| 287 | */ |
| 288 | |
| 289 | @Beta |
| 290 | public static <E> Multiset<E> filter(Multiset<E> unfiltered, Predicate<? super E> predicate) { |
| 291 | if (unfiltered instanceof FilteredMultiset) { |
| 292 | // Support clear(), removeAll(), and retainAll() when filtering a filtered |
| 293 | // collection. |
| 294 | FilteredMultiset<E> filtered = (FilteredMultiset<E>) unfiltered; |
| 295 | Predicate<E> combinedPredicate = Predicates.<E>and(filtered.predicate, predicate); |
| 296 | return new FilteredMultiset<E>(filtered.unfiltered, combinedPredicate); |
| 297 | } |
| 298 | return new FilteredMultiset<E>(unfiltered, predicate); |
| 299 | } |
| 300 | |
| 301 | private static final class FilteredMultiset<E> extends AbstractMultiset<E> { |
| 302 | final Multiset<E> unfiltered; |