Returns a stream consisting of elements that match the predicate.
Syntax
stream.filter(predicate)Example
Enter values below to update the example in real time.
List→nums→of→evens→stream→filter→collect→Collectors→toList→System→out→println→List<Integer> nums = List.of(1, 2, 3, 4, 5, 6);
List<Integer> evens = nums.stream()
.filter(n -> n % 2 == 0)
.collect(Collectors.toList());
System.out.println(evens); // [2, 4, 6]