Count frequency of elements in a List in Java
This post will discuss how to count the frequency of the elements in a list in Java.
1. Using a Set
We know that set stores only distinct entries. The idea is to get distinct elements in the list by inserting all elements in the set & then call static method frequency(Collection<?> c, Object o) provided by the Collections class for each distinct element. frequency() returns the total number of occurrences of the specified element in the list.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.*; // Program to count the frequency of the elements in a list class Main { public static void main(String[] args) { List<String> list = Arrays.asList("B", "A", "A", "C", "B", "A"); Set<String> distinct = new HashSet<>(list); for (String s: distinct) { System.out.println(s + ": " + Collections.frequency(list, s)); } } } |
Output:
A: 3
B: 2
C: 1
2. Using a Map
Instead of storing the distinct elements in the set and then calling Collections.frequency() for each distinct element, we can construct a map that stores the frequencies of the elements present in a list.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; // Program to count the frequency of the elements in a list class Main { public static void main(String[] args) { List<String> list = Arrays.asList("B", "A", "A", "C", "B", "A"); Map<String, Integer> frequencyMap = new HashMap<>(); for (String s: list) { Integer count = frequencyMap.get(s); if (count == null) { count = 0; } frequencyMap.put(s, count + 1); } for (Map.Entry<String, Integer> entry: frequencyMap.entrySet()) { System.out.println(entry.getKey() + ": " + entry.getValue()); } } } |
Output:
A: 3
B: 2
C: 1
We can even simplify things by using streams in Java 8 and above:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.function.Function; import java.util.stream.Collectors; // Program to count the frequency of the elements in a list class Main { public static void main(String[] args) { List<String> list = Arrays.asList("B", "A", "A", "C", "B", "A"); Map<String, Long> frequencyMap = list.stream() .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); for (Map.Entry<String, Long> entry: frequencyMap.entrySet()) { System.out.println(entry.getKey() + ": " + entry.getValue()); } } } |
Output:
A: 3
B: 2
C: 1
Here’s a version without streams (works with Java 8 and above):
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; class Main { public static void main(String[] args) { List<String> list = Arrays.asList("B", "A", "A", "C", "B", "A"); Map<String, Long> frequencyMap = new HashMap<>(); for (String s : list) { frequencyMap.merge(s, 1L, Long::sum); } for (Map.Entry<String, Long> entry: frequencyMap.entrySet()) { System.out.println(entry.getKey() + ": " + entry.getValue()); } } } |
Output:
A: 3
B: 2
C: 1
That’s all about counting the frequency of elements in a List in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)