Is there a way to apply fixed amount of Predicates to an opened Stream ? I really can't get any attempt of mine to work. Either the attempt ends up with stream closed error or not all filters are applied.
Example:
// list of all JLabels of any container
private final List<JLabel> listOfLabels = new ArrayList<>();
// set of user-defined filters
private final Set<Predicate<JLabel>> filters = new HashSet<>();
// let's add some filters
filters.add(label -> label.getBackground() == Color.RED);
filters.add(label -> label.getWidth() > 500);
How can I apply all filter to the Stream of listOfFiles ? Let's say we want to hide JLabels NOT matching these filters. I am looking for something like a non-working code snippet below.
public void applyFilters() {
listOfLabels.forEach(label -> label.setVisible(false)); // hide all labels
Stream<JLabel> stream = listOfLabels.stream();
filters.forEach(stream::filter);
stream.forEach(label -> label.setVisible(true));
// stream closed error
}
After method applyFilters() is executed, the container should have visible only labels matching all predicates defined by filters set. (red background and width greater than 500 in this example).