Java - creating a loop for a filtering function

DrAhzek :

I'm trying to figure out how to handle filtering project. I'm capable of filtering my collection by one field at a time (thanks to the guava Immutable.of) but I'm forced to create a way to filter my collection once again and again, until the user tells (for example, in switch) that he's done and wants to go back to the main menu.

private String filterOption() {
    System.out.println("What fields should I filter by? :\n" +
            "title\n" +
            "author\n" +
            "content\n" +
            "date\n");
    String filteringOption = scanner.nextLine();
    return filteringOption;
}

private String filterValue() {
    System.out.println("What value you want to use for filtering? :\n");
    String userInput = scanner.nextLine();
    return userInput;
}

public List<Message> filterMessages() {

    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())
    );

    BiPredicate<String, Message> predicate = criteria.get(filteringOption);

    filteredMessages = messageStorage.getAll().stream() //getAll() is to get entire collection
            .filter(m -> predicate.test(userInput, m))
            .collect(Collectors.toList());

    return filteredMessages;
}

Could someone show me a way to will allow user to use the values they've got from the filterMessages() to once again filter them using the same method? In short - filter what was once filtered until you are happy or there's nothing in the returned list.

Naman :

Possibly something like :

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();
}

where I've just tweaked your filterMessages method as:

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());
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=76990&siteId=1