1 回答
TA贡献1858条经验 获得超8个赞
可能是这样的:
List<Message> messageStorage = new ArrayList<>(); // messageStorage.getAll()
String userChoice = "";
while (!userChoice.equalsIgnoreCase("No")) {
messageStorage = filterMessages(messageStorage); // modifying the existing list
System.out.println("Want to continue filtering? Yes/No");
userChoice = scanner.next();
}
我刚刚将您的filterMessages方法调整为:
List<Message> filterMessages(List<Message> messageStorage) {
String filteringOption = filterOption();
String userInput = filterValue();
Map<String, BiPredicate<String, Message>> criteria = ImmutableMap.of(
"title", (userTitle, message) -> userInput.equals(message.getTitle()),
"author", (userAuthor, message) -> userInput.equals(message.getAuthor()),
"content", (userContent, message) -> userInput.equals(message.getContent()),
"date", (userDate, message) -> userInput.equals(message.getCreationDate())
);
return messageStorage.stream()
.filter(m -> criteria.get(filteringOption).test(userInput, m))
.collect(Collectors.toList());
}
添加回答
举报