Logo

Programming-Idioms

  • Java

Idiom #57 Filter list

Create the list y containing the items from the list x that satisfy the predicate p. Respect the original ordering. Don't modify x in-place.

List<T> y = x.stream().filter(p).toList();
import java.util.ArrayList;
import java.util.List;
List<T> y = new ArrayList<>();
for (T t : x) if (p.test(t)) y.add(t);
for Item of X loop
   if P (Item) then
      Y.Append (Item);
   end if;
end loop;

New implementation...