I am trying to perform operation for a post (question) in my project,
List<Post> acceptedAnswer = question.getAnswers().stream()
.filter(ans -> Objects.equals(ans.getStatus(), PostStatus.ACCEPTED))
.toList();
if(acceptedAnswer.size() != 1) {
log.error("Invalid accepted answers for a closed question {}", question.getId());
}
is this correct way to retrieve the accepted answer (each question can have atmost 1), in earlier logics i have made sure there cannot be more than 1 accepted answer so do i need this additional check, if not how do i code to receive single value instead of list