First of all, if the set is empty, the lambda passed to forEach won't be executed: an empty stream is empty, and filtering it won't add any element to it. Only potentially remove some.
Second, the lambda creates an exception. But it doesn't throw it.
You can use
if (clients.isEmpty() || clients.stream().anyMatch(client -> !myMay.containsKey(client))) {
throw new InvalidClientException();
}
EDIT: I missed the fact that you wanted to pass the (first?) client not in the set to the exception. To do that, you can do
if (clients.isEmpty()) {
throw new InvalidClientException();
}
clients.stream()
.filter(client -> !myMay.containsKey(client))
.findAny() // or .findFirst()
.ifPresent(client -> {
throw new InvalidClientException(client);
});
That will only work if the exception is a runtime exception, though, because you can't throw a checked exception from a Consumer. If it's a checked exception and you really want to keep it a checked exception, you can use
if (clients.isEmpty()) {
throw new InvalidClientException();
}
Optional<String> wrongClient =
clients.stream()
.filter(client -> !myMay.containsKey(client))
.findAny();
if (wrongClient.isPresent()) {
throw new InvalidClientException(wrongClient.get());
}