No instance(s) of type variable(s) U exist so that void conforms to U
Optional.map()
expects a Function<? super T, ? extends U>
as parameter. Since your method returns void
, it cannot be used like this in the lambda.
I see three options here:
- make that method return
Void
/Object
/whatever instead – semantically not ideal but it would work - make that method return the exception, and change the lambda definition to
.map(v -> { throw printAndReturnException(); });
- use
ifPresent()
, and move theorElseThrow()
outside of the call chain:values.stream() .filter(value -> getDetails(value).getName.equals("TestVal")) .findFirst() .ifPresent(value -> printAndThrowException(value)) throw new Exception2("xyz");