JB Nizet's answer is fine, but it uses map only for its side effects, and not for the map operation, which is rather strange. There is a method that you can use when you are only interested in the side effects of something, for example, throwing an exception: peek .
List<Parent> filtered = list.stream() .peek(Objects::requireNonNull) .filter(predicate) .collect(Collectors.toList());
And if you want your own exception to just put lambda there:
List<Parent> filtered = list.stream() .peek(p -> { if (p == null) throw new MyException(); }) .filter(predicate) .collect(Collectors.toList());
Checked Exceptions
If your exception is checked, you can either check zero in advance, if you don't mind going to the list twice. This is probably best in your case, but may not always be possible.
if (list.contains(null)) throw new MyCheckedException();
You can also throw the thrown exception into the stream pipeline, catch it, and then throw the checked flag:
try { ... .peek(p -> { if (p == null) throw new MyException(); }) ... } catch (MyException exc) { throw new MyCheckedException(); }
Sneaky throw
Or you can take an elegant but controversial road and use the covert throw method.
But be careful! This method bypasses the proven exception system and you should know what you are doing. Be sure to declare that the surrounding method throws a MyCheckedException ! The compiler will not warn you if you do not, and this will most likely cause strange errors if exceptions are noted, if they are not expected.
@SuppressWarnings("unchecked") public <T extends Throwable> void throwSneakily(Throwable t) throws T { throw (T) t; } public void m() throws MyCheckedException { List<Parent> filtered = list.stream() .peek(p -> { if (p == null) throwSneakily(new MyCheckedException()); }) .filter(predicate) .collect(Collectors.toList()); }