@Test public void testCount() throws Exception { int countLargerThan2 = CollectionHelper.count(testCollection, new Predicate<Integer>() { @Override public boolean apply(Integer object) { return object > 2; } }); assertThat(countLargerThan2, is(4)); int countLargerThan50 = CollectionHelper.count(testCollection, new Predicate<Integer>() { @Override public boolean apply(Integer object) { return object > 50; } }); assertThat(countLargerThan50, is(0)); int countPositive = CollectionHelper.count(testCollection, new Predicate<Integer>() { @Override public boolean apply(Integer object) { return object >= 0; } }); assertThat(countPositive, is(testCollection.size())); }
public static <T> int count(Collection<T> items, Predicate<T> predicate) { int count = 0; if (!isEmpty(items)) { for (T element : items) { if (predicate.apply(element)) { count++; } } } return count; }
CollectionHelper { public static <T> int count(Collection<T> items, Predicate<T> predicate) { int count = 0; if (!isEmpty(items)) { for (T element : items) { if (predicate.apply(element)) { count++; } } } return count; } }
CollectionHelper { public static <T> int count(Collection<T> items, Predicate<T> predicate) { int count = 0; if (!isEmpty(items)) { for (T element : items) { if (predicate.apply(element)) { count++; } } } return count; } private  CollectionHelper(); }
CollectionHelper { public static <T> int count(Collection<T> items, Predicate<T> predicate) { int count = 0; if (!isEmpty(items)) { for (T element : items) { if (predicate.apply(element)) { count++; } } } return count; } private  CollectionHelper(); static List<T> filter(Collection<T> items, Predicate<T> predicate); static T first(Collection<T> items, Predicate<T> predicate); static T firstOrNull(Collection<T> items, Predicate<T> predicate); static int firstIndexOf(Collection<T> items, Predicate<T> predicate); static boolean any(Collection<T> items, Predicate<T> predicate); static boolean all(Collection<T> items, Predicate<T> predicate); static boolean isEmpty(Collection items); static T single(Collection<T> items, Predicate<T> predicate); static T singleOrNull(Collection<T> items, Predicate<T> predicate); static int singleIndexOf(Collection<T> items, Predicate<T> predicate); static int count(Collection<T> items, Predicate<T> predicate); static List<TResult> map(Collection<TSource> items, Mapper<TSource, TResult> mapper); }
CollectionHelper { public static <T> int count(Collection<T> items, Predicate<T> predicate) { int count = 0; if (!isEmpty(items)) { for (T element : items) { if (predicate.apply(element)) { count++; } } } return count; } private  CollectionHelper(); static List<T> filter(Collection<T> items, Predicate<T> predicate); static T first(Collection<T> items, Predicate<T> predicate); static T firstOrNull(Collection<T> items, Predicate<T> predicate); static int firstIndexOf(Collection<T> items, Predicate<T> predicate); static boolean any(Collection<T> items, Predicate<T> predicate); static boolean all(Collection<T> items, Predicate<T> predicate); static boolean isEmpty(Collection items); static T single(Collection<T> items, Predicate<T> predicate); static T singleOrNull(Collection<T> items, Predicate<T> predicate); static int singleIndexOf(Collection<T> items, Predicate<T> predicate); static int count(Collection<T> items, Predicate<T> predicate); static List<TResult> map(Collection<TSource> items, Mapper<TSource, TResult> mapper); static final int NOT_FOUND_INDEX; }