Java List filter by date

Light :

I try use my first filter in FilteredList but of course it doesn't work.

public class ListRemove {

    public static void main(String[] args) {

        ObservableList<CashBalance> cashBalanceList = FXCollections.observableArrayList();

        LocalDate f1 = LocalDate.of(2011, Month.JANUARY, 1);
        LocalDate f2 = LocalDate.of(2015, Month.AUGUST, 2);

        CashBalance c1 = new CashBalance();
        CashBalance c2 = new CashBalance();
        c1.setData(f1);
        c2.setData(f2);

        cashBalanceList.setAll(c1, c2);

        FilteredList<CashBalance> filteredList = new FilteredList<CashBalance>(cashBalanceList);

        filteredList.stream().filter(p -> p.getData().isAfter(LocalDate.of(2012, Month.JUNE, 2)))
                .collect(Collectors.toList());

        for (CashBalance l : filteredList) {

            System.out.println(l.getData());

        }
    }
}

Should show one date but two are displayed. What am I doing wrong ?

puhlen :

Your approach does not filter the list, but copies the elements to a new list, with some elements filtered out. This leaves the original unchanged. You need to assign this new list to a variable to make use of it.

However, you are using the FilteredList class, which provides a filtered view of another list. You can set the filter predicate using the setPredicate method

filteredList.setPredicate(p -> p.getData().isAfter(LocalDate.of(2012, Month.JUNE, 2)));

Or in the constructor as the second parameter

 FilteredList<CashBalance> filteredList = new FilteredList<CashBalance>(cashBalanceList, p -> p.getData().isAfter(LocalDate.of(2012, Month.JUNE, 2)));

Guess you like

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