I have created a method that checks if there is an element in the list with a specific id, and if it does not exist it throws an exception. I just do not know how to check if the match value is false, then throw an my exception AccessToMessageForbiddenException.
private void validMessage(Long messageId) { List<Message> messageList = messageService.findBySender(1L); messageList.addAll(messageService.findByRecipient(1L)); boolean match = messageList.stream().anyMatch(v -> messageId.equals(v.getId())); //How to throw an exception without an if construct }