Java ProgramsCollectionsCollection Frequency

Collection Frequency in Java

beginner·  Collections  ·  Collections Utility

Problem

Collections.frequency() answers a narrower question than a full frequency map does — it counts occurrences of exactly one given value, without tallying every distinct value in the collection.

Given a List of integers, count how many times one specific value appears in it.

Input
[4, 7, 4, 2, 4, 9, 7], target = 4
Output
Frequency of 4: 3

Java Program

Java
import java.util.Arrays; import java.util.Collections; import java.util.List; public class CollectionFrequency { public static void main(String[] args) { List<Integer> numbers = Arrays.asList(4, 7, 4, 2, 4, 9, 7); int count = Collections.frequency(numbers, 4); // counts only how many elements equal 4 System.out.println("Frequency of 4: " + count); } }

Output

Frequency of 4: 3

Core Logic

Passing the list and the target value to Collections.frequency() counts matching elements in one call, instead of a manual loop with its own counter.

How It Works
  1. 1Arrays.asList(4, 7, 4, 2, 4, 9, 7) builds the list of numbers to search.
  2. 2Collections.frequency(numbers, 4) compares every element against 4 using equals(), counting how many match.
  3. 3The method returns a plain int count — 3 here, since 4 appears three times.
  4. 4Nothing about the rest of the list's values matters — frequency() only reports on the one target value it was asked about.
In [4, 7, 4, 2, 4, 9, 7], the value 4 appears at three positions, so Collections.frequency(numbers, 4) returns 3.
💡

Key Point: This is a different task from building a full frequency map of every distinct value — frequency() is the right tool when only one specific value's count is actually needed, since it skips tracking the rest entirely.

Complexity
Time Complexity: O(n)Space Complexity: O(1)

Why: Collections.frequency() makes a single linear scan comparing every element to the target value, using only a running counter regardless of the list's contents.

Key Concepts

Collections.frequency()Listutility method

Approach 2: Java 8

Java
import java.util.Arrays; import java.util.List; public class CollectionFrequencyStream { public static void main(String[] args) { List<Integer> numbers = Arrays.asList(4, 7, 4, 2, 4, 9, 7); // Keeps only the matching elements, then tallies how many survived long count = numbers.stream().filter(n -> n == 4).count(); System.out.println("Frequency of 4: " + count); } }

Output

Frequency of 4: 3

Core Logic

Filtering the stream down to only the matching elements, then counting what's left, expresses the same 'how many equal this value' question as a pipeline.

How It Works
  1. 1numbers.stream() opens a stream over the list's elements.
  2. 2.filter(n -> n == 4) keeps only the elements equal to the target value, discarding everything else.
  3. 3.count() reduces the filtered stream down to how many elements survived — a long, not an int.
  4. 4No intermediate list of the matches is ever built — count() tallies as it filters.
Filtering [4, 7, 4, 2, 4, 9, 7] down to just the 4s keeps three elements, so count() returns 3.
💡

Key Point: count() returns a long rather than an int — a minor but real difference from Collections.frequency()'s plain int return, worth knowing if the result feeds into further arithmetic.

Complexity
Time Complexity: O(n)Space Complexity: O(1)

Why: filter() and count() together still make a single pass over the n elements, tallying matches without collecting them into a separate structure.

Key Concepts

Streamfilter()count()

Related Programs