Collection Frequency in Java
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.
Java Program
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
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.
- 1
Arrays.asList(4, 7, 4, 2, 4, 9, 7)builds the list of numbers to search. - 2
Collections.frequency(numbers, 4)compares every element against4usingequals(), counting how many match. - 3The method returns a plain
intcount —3here, since4appears three times. - 4Nothing about the rest of the list's values matters —
frequency()only reports on the one target value it was asked about.
[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.
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
Approach 2: Java 8
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
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.
- 1
numbers.stream()opens a stream over the list's elements. - 2
.filter(n -> n == 4)keeps only the elements equal to the target value, discarding everything else. - 3
.count()reduces the filtered stream down to how many elements survived — along, not anint. - 4No intermediate list of the matches is ever built —
count()tallies as it filters.
[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.
Why: filter() and count() together still make a single pass over the n elements, tallying matches without collecting them into a separate structure.