Lucas Pires Lucas Pires - 1 year ago 122
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?

Answer Source

I think what you want is simply:

      () -> new RuntimeException("No group")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download