Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Guava: Iterables.frequency(Iterable<T>, Predicate<T>)

Tags:

java

guava

Is there really no method that determines the number of elements that satisfy a Predicate in an Iterable? Was I right to do this:

return Lists.newArrayList(Iterables.filter(iterable, predicate)).size()

If so, what is the reason that there is no method

Iterable.frequency(Iterable<T>, Predicate<T>)

Cheers

like image 977
kungfoo Avatar asked Dec 08 '10 13:12

kungfoo


1 Answers

This may be easier:

return Iterables.size(Iterables.filter(iterable, predicate));

It avoids the allocation of all that array memory.

like image 57
Adam Paynter Avatar answered Sep 20 '22 20:09

Adam Paynter