amaembo / streamex

Enhancing Java Stream API
Apache License 2.0
2.19k stars 251 forks source link

Implement count(Predicate<T>) #260

Closed Suicolen closed 2 years ago

Suicolen commented 2 years ago

I believe it is somewhat common to filter a stream and count the number of elements, for example to count the number of even numbers in a range u would write: long evenCount = IntStreamEx.range(n).filter(x -> x % 2 == 0).count(); However a count method that takes in a Predicate and only counts the element if it matches the predicate would be more convenient imo. Example: long evenCount = IntStreamEx.range(n).count(x -> x % 2 == 0);

Alternatively the method could be called countIf