Index: luni/src/test/java/org/apache/harmony/tests/java/util/CollectionsTest.java =================================================================== --- luni/src/test/java/org/apache/harmony/tests/java/util/CollectionsTest.java (revision 379882) +++ luni/src/test/java/org/apache/harmony/tests/java/util/CollectionsTest.java (working copy) @@ -17,6 +17,7 @@ package org.apache.harmony.tests.java.util; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.LinkedList; import java.util.List; @@ -195,4 +196,33 @@ // expected } } + + /** + * @tests java.util.Collections#frequency(java.util.Collection,Object) + */ + public void test_frequencyLjava_util_CollectionLint() { + try { + Collections.frequency(null,null); + fail("Assert 0: frequency(null,) must throw NPE"); + } catch (NullPointerException e) {} + + + List strings = Arrays.asList(new String[] {"1", "2", "3", "1", "1"}); + + assertEquals(3, Collections.frequency(strings, "1")); + + assertEquals(1, Collections.frequency(strings, "2")); + + assertEquals(1, Collections.frequency(strings, "3")); + + assertEquals(0, Collections.frequency(strings, null)); + + List objects = Arrays.asList(new Object[] {new Integer(1), null, null, new Long(1)}); + + assertEquals(1, Collections.frequency(objects, new Integer(1))); + + assertEquals(1, Collections.frequency(objects, new Long(1))); + + assertEquals(2, Collections.frequency(objects, null)); + } } Index: luni/src/main/java/java/util/Collections.java =================================================================== --- luni/src/main/java/java/util/Collections.java (revision 379778) +++ luni/src/main/java/java/util/Collections.java (working copy) @@ -2151,4 +2151,35 @@ throw new NullPointerException(); return new UnmodifiableSortedSet(set); } + + /** + *

+ * Returns the number of elements in the Collection that + * match the Object passed. If the Object is + * null, then the number of null elements is + * returned. + *

+ * + * @param c The Collection to search. + * @param o The Object to search for. + * @return The number of matching elements. + * @throws NullPointerException if the Collection parameter + * is null. + * + * @since 1.5 + */ + public static int frequency(Collection c, Object o) { + if (c == null) + throw new NullPointerException(); + if (c.isEmpty()) + return 0; + int result = 0; + Iterator itr = c.iterator(); + while (itr.hasNext()) { + Object e = itr.next(); + if (o == null ? e == null : o.equals(e)) + result++; + } + return result; + } }