amaembo / streamex

Enhancing Java Stream API
Apache License 2.0
2.18k stars 249 forks source link

Implement negated filter method #257

Closed Su5eD closed 2 years ago

Su5eD commented 2 years ago

The Stream#filter method is commonly used with a negated operation, which requires using the negation operator ! and prevents shortening code with method references. I propose the introduction of Stream#filterNot to eliminate this issue.

A simple implementation and use case may be as follows:

// one.util.streamex.AbstractStreamEx

public S filterNot(Predicate<? super T> predicate) {
    return filter(predicate.negate());
}
public static void givenDays_returnWeekDays() {
    List<String> days = Arrays.asList("mon", "tue", "wed", "thu", "fri", "sat", "sun");
    List<String> weekend = Arrays.asList("sat", "sun");

    List<String> weekdays = StreamEx.of(days).filterNot(weekend::contains).toList(); 
}
Suicolen commented 2 years ago

Such function already exists in AbstractStreamEx and it's called remove instead