Home Java Collections Methods frequency() Java Collections frequency() Method Definition In Java, Collections.frequency(c, o) returns the number of elements in collection c that are equal to o. Equality means o.equals(element), or element == null when o is null.
It answers questions like "how many votes said yes?" or "how many times does this letter appear?" in one line, and returns 0 when the value is absent.
A static method of the java.util.Collections utility class, available since Java 5. Call it on the class name, as in Collections.frequency(...), after import java.util.Collections;. You never create a Collections object.
Syntax Pass the collection and the value to count.
List<String> votes = Arrays.asList("yes", "no", "yes");
int yes = Collections.frequency(votes, "yes"); // 2
int maybe = Collections.frequency(votes, "maybe"); // 0 Call What it does Collections.frequency(c, o)Number of elements equal to o. Collections.frequency(c, null)Number of null elements. Collections.frequency(c, o) > 0Same as c.contains(o), but slower. Collections.frequency(c, o) > 1Checks whether o is duplicated.
frequency always scans the whole collection, so it takes linear time even on a HashSet, where the answer can only be 0 or 1. To count every distinct value at once, a single loop that fills a map is faster than calling frequency once per value.
Library declarations // java.util.Collections
public static int frequency(Collection<?> c, Object o) Parameters Parameter Description cThe collection to count in. oThe value to count. May be null.
Return Value Return Value Description intHow many elements are equal to o. 0 if none.
Important
A null collection throws NullPointerException. Because matching uses equals, the value's type matters: Long 5 is not equal to Integer 5, and "Yes" is not equal to "yes". Custom classes need a proper equals method to be counted correctly.
Compare with Similar Methods Related ways to test for values:
Java Examples 1. Count votes 2. Count each letter 3. Equality traps Example 1 — Count Votes for Each Answer Tally a list of poll answers, including one nobody chose.
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class CollectionsFrequencyVotes {
public static void main(String[] args) {
List<String> votes = Arrays.asList("yes", "no", "yes", "yes", "abstain");
System.out.println("yes: " + Collections.frequency(votes, "yes"));
System.out.println("no: " + Collections.frequency(votes, "no"));
System.out.println("maybe: " + Collections.frequency(votes, "maybe"));
System.out.println("total: " + votes.size());
}
} yes: 3
no: 1
maybe: 0
total: 5 How It Works Three matches. "yes" appears at indexes 0, 2, and 3.Absent value. Nobody voted "maybe", so the count is 0 rather than an error.Full scan. Each call looks at all five votes, which is fine for small lists.Example 2 — Count Every Distinct Letter in a Word Turn "banana" into a list of characters, then count each distinct letter in alphabetical order.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.TreeSet;
public class CollectionsFrequencyLetters {
public static void main(String[] args) {
List<Character> letters = new ArrayList<>();
for (char ch : "banana".toCharArray()) {
letters.add(ch);
}
for (Character ch : new TreeSet<>(letters)) {
System.out.println(ch + " = " + Collections.frequency(letters, ch));
}
}
} How It Works Box each char. The loop adds the six characters of "banana" to a List<Character>.Distinct and sorted. Copying into a TreeSet keeps one copy of each letter in alphabetical order: a, b, n.Count against the full list. For each distinct letter, frequency scans the original six characters and finds 3, 1, and 2.Example 3 — null, Case, and Integer vs Long See how equals decides what counts: null values, letter case, and boxed number types.
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class CollectionsFrequencyEquality {
public static void main(String[] args) {
List<String> answers = Arrays.asList("Yes", null, "yes", null);
System.out.println("null: " + Collections.frequency(answers, null));
System.out.println("\"yes\": " + Collections.frequency(answers, "yes"));
List<Integer> nums = Arrays.asList(5, 5, 7);
System.out.println("5: " + Collections.frequency(nums, 5));
System.out.println("5L: " + Collections.frequency(nums, 5L));
}
} null: 2
"yes": 1
5: 2
5L: 0 How It Works Counting null. When the value is null, frequency compares with == and finds two empty answers.Case counts. "Yes" with a capital Y is not equal to "yes", so only one answer matches.Boxed type matters. 5 boxes to an Integer and matches twice, but 5L boxes to a Long, and Long.equals is false for any Integer.Frequently Asked Questions What does Collections.frequency() return? The number of elements in the collection that are equal to the given value, or 0 if there are none.
Can Collections.frequency() count null values? Yes. Pass null as the value and it counts the null elements.
Is Collections.frequency() case-sensitive for strings? Yes, because String.equals is case-sensitive. Normalize the case first if you want to ignore it.
Why does frequency(list, 5L) return 0 for a list of Integer? 5L is a Long, and a Long is never equal to an Integer, even with the same numeric value.
How do I count every distinct element efficiently? Loop once and update a map, for example counts.merge(x, 1, Integer::sum). Calling frequency for every value scans the list again each time.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
15 people found this page helpful
Helpful Share Copy link Suggestion