filterView

inline fun <E> Collection<E>.filterView(noinline predicate: (E) -> Boolean): Collection<E>(source)

Returns the elements of this that satisfy a predicate. The returned collection is a live view of this; changes to one affect the other.

The returned collection isn't threadsafe or serializable, even if this is.

Many of the filtered collection's methods, such as size, iterate across every element in the underlying collection and determine which elements satisfy the filter. When a live view is not needed, it may be faster to copy Iterables.filter(unfiltered, predicate) and use the copy.

Warning: predicate must be consistent with equals, as documented at Predicate.apply. Do not provide a predicate such as Predicates.instanceOf(ArrayList.class), which is inconsistent with equals. (See Iterables.filter for related functionality.)

Stream equivalent: Stream.filter.

See also


@JvmName(name = "filterMutable")
inline fun <E> MutableCollection<E>.filterView(noinline predicate: (E) -> Boolean): MutableCollection<E>(source)

Returns the elements of this that satisfy a predicate. The returned collection is a live view of this; changes to one affect the other.

The resulting collection's iterator does not support remove, but all other collection methods are supported. When given an element that doesn't satisfy the predicate, the collection's add and addAll methods throw an IllegalArgumentException. When methods such as removeAll and clear are called on the filtered collection, only elements that satisfy the filter will be removed from the underlying collection.

The returned collection isn't threadsafe or serializable, even if this is.

Many of the filtered collection's methods, such as size, iterate across every element in the underlying collection and determine which elements satisfy the filter. When a live view is not needed, it may be faster to copy Iterables.filter(unfiltered, predicate) and use the copy.

Warning: predicate must be consistent with equals, as documented at Predicate.apply. Do not provide a predicate such as Predicates.instanceOf(ArrayList.class), which is inconsistent with equals. (See Iterables.filter for related functionality.)

Stream equivalent: Stream.filter.

See also


inline fun <T> Iterable<T>.filterView(noinline retainIfTrue: (T) -> Boolean): Iterable<T>(source)

Returns a view of this containing all elements that satisfy the input predicate retainIfTrue.

Stream equivalent: Stream.filter.

See also


inline fun <T> Iterator<T>.filterView(noinline retainIfTrue: (T) -> Boolean): Iterator<T>(source)

Returns a view of this containing all elements that satisfy the input predicate retainIfTrue.

See also