Lucas Pires Lucas Pires - 7 months ago 40
Java Question

How to return orElseThrow in a same condition with ifPresent using Java 8?

I have a Java 8 code below using ifPresent, I want in the same condition to use orElseThrow

Already returns a Optional object.

public void forwardMessageGroup(String group, ChatMessage message) {

chatSessionStore.get(group).ifPresent(chatProxy -> chatProxy.sendMessage(message));


Any tips?


I think what you want is simply:

      () -> new RuntimeException("No group")