Will Java 8 create a new List after using Stream "filter" and "collect"?
If you actually want to modify the original list, consider using removeIf
:
list.removeIf(i -> i < 2);
According to the Javadoc, passing the Collector
returned by Collectors.toList()
into the collect
method will create a new list.
public static <T> Collector<T,?,List<T>> toList()
Returns a
Collector
that accumulates the input elements into a newList
. There are no guarantees on the type, mutability, serializability, or thread-safety of theList
returned; if more control over the returnedList
is required, usetoCollection(Supplier)
.
The original list remains unaffected.