@Test public void frequency() { assertEquals(0, IterableUtils.frequency(null, 1)); assertEquals(1, IterableUtils.frequency(iterableA, 1)); assertEquals(2, IterableUtils.frequency(iterableA, 2)); assertEquals(3, IterableUtils.frequency(iterableA, 3)); assertEquals(4, IterableUtils.frequency(iterableA, 4)); assertEquals(0, IterableUtils.frequency(iterableA, 5)); assertEquals(0, IterableUtils.frequency(iterableB, 1L)); assertEquals(4, IterableUtils.frequency(iterableB, 2L)); assertEquals(3, IterableUtils.frequency(iterableB, 3L)); assertEquals(2, IterableUtils.frequency(iterableB, 4L)); assertEquals(1, IterableUtils.frequency(iterableB, 5L)); Iterable<Number> iterableIntAsNumber = Arrays.<Number>asList(1, 2, 3, 4, 5); Iterable<Number> iterableLongAsNumber = Arrays.<Number>asList(1L, 2L, 3L, 4L, 5L); assertEquals(0, IterableUtils.frequency(iterableIntAsNumber, 2L)); assertEquals(0, IterableUtils.frequency(iterableLongAsNumber, 2)); final Set<String> set = new HashSet<String>(); set.add("A"); set.add("C"); set.add("E"); set.add("E"); assertEquals(1, IterableUtils.frequency(set, "A")); assertEquals(0, IterableUtils.frequency(set, "B")); assertEquals(1, IterableUtils.frequency(set, "C")); assertEquals(0, IterableUtils.frequency(set, "D")); assertEquals(1, IterableUtils.frequency(set, "E")); final Bag<String> bag = new HashBag<String>(); bag.add("A", 3); bag.add("C"); bag.add("E"); bag.add("E"); assertEquals(3, IterableUtils.frequency(bag, "A")); assertEquals(0, IterableUtils.frequency(bag, "B")); assertEquals(1, IterableUtils.frequency(bag, "C")); assertEquals(0, IterableUtils.frequency(bag, "D")); assertEquals(2, IterableUtils.frequency(bag, "E")); }
public static <E, T extends E> int frequency(final Iterable<E> iterable, final T obj) { if (iterable instanceof Set<?>) { return ((Set<E>) iterable).contains(obj) ? 1 : 0; } if (iterable instanceof Bag<?>) { return ((Bag<E>) iterable).getCount(obj); } return size(filteredIterable(emptyIfNull(iterable), EqualPredicate.<E>equalPredicate(obj))); }
IterableUtils { public static <E, T extends E> int frequency(final Iterable<E> iterable, final T obj) { if (iterable instanceof Set<?>) { return ((Set<E>) iterable).contains(obj) ? 1 : 0; } if (iterable instanceof Bag<?>) { return ((Bag<E>) iterable).getCount(obj); } return size(filteredIterable(emptyIfNull(iterable), EqualPredicate.<E>equalPredicate(obj))); } }
IterableUtils { public static <E, T extends E> int frequency(final Iterable<E> iterable, final T obj) { if (iterable instanceof Set<?>) { return ((Set<E>) iterable).contains(obj) ? 1 : 0; } if (iterable instanceof Bag<?>) { return ((Bag<E>) iterable).getCount(obj); } return size(filteredIterable(emptyIfNull(iterable), EqualPredicate.<E>equalPredicate(obj))); }  }
IterableUtils { public static <E, T extends E> int frequency(final Iterable<E> iterable, final T obj) { if (iterable instanceof Set<?>) { return ((Set<E>) iterable).contains(obj) ? 1 : 0; } if (iterable instanceof Bag<?>) { return ((Bag<E>) iterable).getCount(obj); } return size(filteredIterable(emptyIfNull(iterable), EqualPredicate.<E>equalPredicate(obj))); }  @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Iterable<E> emptyIterable(); @SuppressWarnings("unchecked") static Iterable<E> chainedIterable(final Iterable<? extends E> a,
                                                  final Iterable<? extends E> b); @SuppressWarnings("unchecked") static Iterable<E> chainedIterable(final Iterable<? extends E> a,
                                                  final Iterable<? extends E> b,
                                                  final Iterable<? extends E> c); @SuppressWarnings("unchecked") static Iterable<E> chainedIterable(final Iterable<? extends E> a,
                                                  final Iterable<? extends E> b,
                                                  final Iterable<? extends E> c,
                                                  final Iterable<? extends E> d); static Iterable<E> chainedIterable(final Iterable<? extends E>... iterables); static Iterable<E> collatedIterable(final Iterable<? extends E> a,
                                                   final Iterable<? extends E> b); static Iterable<E> collatedIterable(final Comparator<? super E> comparator,
                                                   final Iterable<? extends E> a,
                                                   final Iterable<? extends E> b); static Iterable<E> filteredIterable(final Iterable<E> iterable,
                                                   final Predicate<? super E> predicate); static Iterable<E> boundedIterable(final Iterable<E> iterable, final long maxSize); static Iterable<E> loopingIterable(final Iterable<E> iterable); static Iterable<E> reversedIterable(final Iterable<E> iterable); static Iterable<E> skippingIterable(final Iterable<E> iterable, final long elementsToSkip); static Iterable<O> transformedIterable(final Iterable<I> iterable,
                                                         final Transformer<? super I, ? extends O> transformer); static Iterable<E> uniqueIterable(final Iterable<E> iterable); static Iterable<E> unmodifiableIterable(final Iterable<E> iterable); static Iterable<E> zippingIterable(final Iterable<? extends E> a,
                                                  final Iterable<? extends E> b); static Iterable<E> zippingIterable(final Iterable<? extends E> first,
                                                  final Iterable<? extends E>... others); static Iterable<E> emptyIfNull(final Iterable<E> iterable); static void forEach(final Iterable<E> iterable, final Closure<? super E> closure); static E forEachButLast(final Iterable<E> iterable, final Closure<? super E> closure); static E find(final Iterable<E> iterable, final Predicate<? super E> predicate); static int indexOf(final Iterable<E> iterable, final Predicate<? super E> predicate); static boolean matchesAll(final Iterable<E> iterable, final Predicate<? super E> predicate); static boolean matchesAny(final Iterable<E> iterable, final Predicate<? super E> predicate); static long countMatches(final Iterable<E> input, final Predicate<? super E> predicate); static boolean isEmpty(final Iterable<?> iterable); static boolean contains(final Iterable<E> iterable, final Object object); static boolean contains(final Iterable<? extends E> iterable, final E object,
                                       final Equator<? super E> equator); static int frequency(final Iterable<E> iterable, final T obj); static T get(final Iterable<T> iterable, final int index); static int size(final Iterable<?> iterable); static List<List<O>> partition(final Iterable<? extends O> iterable,
                                              final Predicate<? super O> predicate); static List<List<O>> partition(final Iterable<? extends O> iterable,
                                              final Predicate<? super O>... predicates); static List<R> partition(final Iterable<? extends O> iterable,
            final Factory<R> partitionFactory, final Predicate<? super O>... predicates); static List<E> toList(final Iterable<E> iterable); static String toString(final Iterable<E> iterable); static String toString(final Iterable<E> iterable,
                                      final Transformer<? super E, String> transformer); static String toString(final Iterable<E> iterable,
                                      final Transformer<? super E, String> transformer,
                                      final String delimiter,
                                      final String prefix,
                                      final String suffix); }
IterableUtils { public static <E, T extends E> int frequency(final Iterable<E> iterable, final T obj) { if (iterable instanceof Set<?>) { return ((Set<E>) iterable).contains(obj) ? 1 : 0; } if (iterable instanceof Bag<?>) { return ((Bag<E>) iterable).getCount(obj); } return size(filteredIterable(emptyIfNull(iterable), EqualPredicate.<E>equalPredicate(obj))); }  @SuppressWarnings("unchecked") // OK, empty collection is compatible with any type static Iterable<E> emptyIterable(); @SuppressWarnings("unchecked") static Iterable<E> chainedIterable(final Iterable<? extends E> a,
                                                  final Iterable<? extends E> b); @SuppressWarnings("unchecked") static Iterable<E> chainedIterable(final Iterable<? extends E> a,
                                                  final Iterable<? extends E> b,
                                                  final Iterable<? extends E> c); @SuppressWarnings("unchecked") static Iterable<E> chainedIterable(final Iterable<? extends E> a,
                                                  final Iterable<? extends E> b,
                                                  final Iterable<? extends E> c,
                                                  final Iterable<? extends E> d); static Iterable<E> chainedIterable(final Iterable<? extends E>... iterables); static Iterable<E> collatedIterable(final Iterable<? extends E> a,
                                                   final Iterable<? extends E> b); static Iterable<E> collatedIterable(final Comparator<? super E> comparator,
                                                   final Iterable<? extends E> a,
                                                   final Iterable<? extends E> b); static Iterable<E> filteredIterable(final Iterable<E> iterable,
                                                   final Predicate<? super E> predicate); static Iterable<E> boundedIterable(final Iterable<E> iterable, final long maxSize); static Iterable<E> loopingIterable(final Iterable<E> iterable); static Iterable<E> reversedIterable(final Iterable<E> iterable); static Iterable<E> skippingIterable(final Iterable<E> iterable, final long elementsToSkip); static Iterable<O> transformedIterable(final Iterable<I> iterable,
                                                         final Transformer<? super I, ? extends O> transformer); static Iterable<E> uniqueIterable(final Iterable<E> iterable); static Iterable<E> unmodifiableIterable(final Iterable<E> iterable); static Iterable<E> zippingIterable(final Iterable<? extends E> a,
                                                  final Iterable<? extends E> b); static Iterable<E> zippingIterable(final Iterable<? extends E> first,
                                                  final Iterable<? extends E>... others); static Iterable<E> emptyIfNull(final Iterable<E> iterable); static void forEach(final Iterable<E> iterable, final Closure<? super E> closure); static E forEachButLast(final Iterable<E> iterable, final Closure<? super E> closure); static E find(final Iterable<E> iterable, final Predicate<? super E> predicate); static int indexOf(final Iterable<E> iterable, final Predicate<? super E> predicate); static boolean matchesAll(final Iterable<E> iterable, final Predicate<? super E> predicate); static boolean matchesAny(final Iterable<E> iterable, final Predicate<? super E> predicate); static long countMatches(final Iterable<E> input, final Predicate<? super E> predicate); static boolean isEmpty(final Iterable<?> iterable); static boolean contains(final Iterable<E> iterable, final Object object); static boolean contains(final Iterable<? extends E> iterable, final E object,
                                       final Equator<? super E> equator); static int frequency(final Iterable<E> iterable, final T obj); static T get(final Iterable<T> iterable, final int index); static int size(final Iterable<?> iterable); static List<List<O>> partition(final Iterable<? extends O> iterable,
                                              final Predicate<? super O> predicate); static List<List<O>> partition(final Iterable<? extends O> iterable,
                                              final Predicate<? super O>... predicates); static List<R> partition(final Iterable<? extends O> iterable,
            final Factory<R> partitionFactory, final Predicate<? super O>... predicates); static List<E> toList(final Iterable<E> iterable); static String toString(final Iterable<E> iterable); static String toString(final Iterable<E> iterable,
                                      final Transformer<? super E, String> transformer); static String toString(final Iterable<E> iterable,
                                      final Transformer<? super E, String> transformer,
                                      final String delimiter,
                                      final String prefix,
                                      final String suffix);  }