Java Collections frequency() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 5+

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.

Java
List<String> votes = Arrays.asList("yes", "no", "yes");
int yes = Collections.frequency(votes, "yes");    // 2
int maybe = Collections.frequency(votes, "maybe"); // 0
CallWhat 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
// java.util.Collections
public static int frequency(Collection<?> c, Object o)

Parameters

ParameterDescription
cThe collection to count in.
oThe value to count. May be null.

Return Value

Return ValueDescription
intHow many elements are equal to o. 0 if none.

Compare with Similar Methods

Related ways to test for values:

MethodHow it differs
contains(o)Only yes or no; stops at the first match.
disjoint(a, b)Checks whether two collections share any element.
replaceAll(list, a, b)Changes every element that frequency would count.
indexOf(o)Position of the first match instead of a count.

Java Examples

Example 1 — Count Votes for Each Answer

Tally a list of poll answers, including one nobody chose.

Java
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());
    }
}

How It Works

  1. Three matches. "yes" appears at indexes 0, 2, and 3.
  2. Absent value. Nobody voted "maybe", so the count is 0 rather than an error.
  3. 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.

Java
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

  1. Box each char. The loop adds the six characters of "banana" to a List<Character>.
  2. Distinct and sorted. Copying into a TreeSet keeps one copy of each letter in alphabetical order: a, b, n.
  3. 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.

Java
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));
    }
}

How It Works

  1. Counting null. When the value is null, frequency compares with == and finds two empty answers.
  2. Case counts. "Yes" with a capital Y is not equal to "yes", so only one answer matches.
  3. 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

The number of elements in the collection that are equal to the given value, or 0 if there are none.
Yes. Pass null as the value and it counts the null elements.
Yes, because String.equals is case-sensitive. Normalize the case first if you want to ignore it.
5L is a Long, and a Long is never equal to an Integer, even with the same numeric value.
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.

Next: indexOfSubList()

Find where a sequence of elements first appears in a list.

indexOfSubList() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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