@Test public void testFilter() throws Exception { List<Integer> largerThan2 = CollectionHelper.filter(testCollection, new Predicate<Integer>() { @Override public boolean apply(Integer object) { return object > 2; } }); assertThat(largerThan2, is(Arrays.asList(4, 7, 8, 5))); List<Integer> largerThan50 = CollectionHelper.filter(testCollection, new Predicate<Integer>() { @Override public boolean apply(Integer object) { return object > 50; } }); assertThat(largerThan50.size(), is(0)); List<Integer> noFiltering = CollectionHelper.filter(testCollection, new Predicate<Integer>() { @Override public boolean apply(Integer object) { return true; } }); assertThat(noFiltering, is(testCollection)); }
public static <T> List<T> filter(Collection<T> items, Predicate<T> predicate) { List<T> result = new ArrayList<>(); if (!isEmpty(items)) { for (T item : items) { if (predicate.apply(item)) { result.add(item); } } } return result; }
CollectionHelper { public static <T> List<T> filter(Collection<T> items, Predicate<T> predicate) { List<T> result = new ArrayList<>(); if (!isEmpty(items)) { for (T item : items) { if (predicate.apply(item)) { result.add(item); } } } return result; } }
CollectionHelper { public static <T> List<T> filter(Collection<T> items, Predicate<T> predicate) { List<T> result = new ArrayList<>(); if (!isEmpty(items)) { for (T item : items) { if (predicate.apply(item)) { result.add(item); } } } return result; } private  CollectionHelper(); }
CollectionHelper { public static <T> List<T> filter(Collection<T> items, Predicate<T> predicate) { List<T> result = new ArrayList<>(); if (!isEmpty(items)) { for (T item : items) { if (predicate.apply(item)) { result.add(item); } } } return result; } 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> List<T> filter(Collection<T> items, Predicate<T> predicate) { List<T> result = new ArrayList<>(); if (!isEmpty(items)) { for (T item : items) { if (predicate.apply(item)) { result.add(item); } } } return result; } 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; }