public Message getMessageByAuthor(String authorName) {
List<Message> nameList = new ArrayList<Message>(messages.values());
Message message = null;
if (!authorName.isBlank()) {
Iterator<Message> iterator = nameList.iterator();
while (iterator.hasNext()) {
message = iterator.next();
if (message.getMessageAuthor().contains(authorName)) {
return message;
}
}
}
return message;
}
As Triby mentioned in the comment, you should create a new List and add the elements that match your criteria and then return the list so that you get more than one element if they match.
public List<Message> getMessageByAuthor(String authorName) {
List<Message> nameList = new ArrayList<Message>(messages.values());
List<Message> responseMessageList = new ArrayList<>();
Message message = null;
if (!authorName.isBlank()) {
Iterator<Message> iterator = nameList.iterator();
while (iterator.hasNext()) {
message = iterator.next();
if (message.getMessageAuthor().contains(authorName)) {
responseMessageList.add(message);
}
}
}
return responseMessageList;
}