In a kind of chat with a chatbot that I'm implementing, I have a List of Conversation and every Conversation has a List of Messages. I want to add those conversations that its last message is not a chatbot message and is not viewed by the user to a List of Conversations. To do that I'm coding it as bellow:
List<ConversationDto> conversationsToBeSent = new ArrayList<>();
for (ConversationDto conversation : super.allConversations) {
if (conversation.getMessages() != null) {
if (conversation.getMessages().get(0).getMessageType().getId() != MessageTypeEnum.CHATBOT.getValue()
&& conversation.getMessages().get(0).getToViewedAt() == null) {
conversationsToBeSent.add(conversation);
}
}
}
This is working. The question is how can I code that using Lambda and stream instead of for each.