Count occurrences of elements of list in Java
Suppose we have an elements in ArrayList, we can count the occurrences of elements present in a number of ways.
This data structure uses hash function to map similar values, known as keys to their associated values. Map values can be retrieved using key as it contains key-value pairs.
// Java program to count frequencies of elements // using HashMap. import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.*; class GFG { public static void countFrequencies(ArrayList<String> list) { // hashmap to store the frequency of element Map<String, Integer> hm = new HashMap<String, Integer>(); for (String i : list) { Integer j = hm.get(i); hm.put(i, (j == null) ? 1 : j + 1); } // displaying the occurrence of elements in the arraylist for (Map.Entry<String, Integer> val : hm.entrySet()) { System.out.println("Element " + val.getKey() + " " + "occurs" + ": " + val.getValue() + " times"); } } public static void main(String[] args) { ArrayList<String> list = new ArrayList<String>(); list.add("Geeks"); list.add("for"); list.add("Geeks"); countFrequencies(list); } } |
Element Geeks occurs: 2 times Element for occurs: 1 times
This data structure does not allow duplicate elements as it implements Set Interface. Objects are inserted based on their hash code.
To count occurrences of elements of ArrayList, we create HashSet and add all the elements of ArrayList. We use Collections.frequency(Collection c, Object o) to count the occurrence of object o in the collection c.
Below program illustrate the working of HashSet:
Program to find occurrence of words
// Java program to count frequencies of elements // using HashSet and Collections.frequency. import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.*; class GFG { public static void countFrequencies(ArrayList<String> list) { // hash set is created and elements of // arraylist are insertd into it Set<String> st = new HashSet<String>(list); for (String s : st) System.out.println(s + ": " + Collections.frequency(list, s)); } public static void main(String[] args) { ArrayList<String> list = new ArrayList<String>(); list.add("Geeks"); list.add("for"); list.add("Geeks"); countFrequencies(list); } } |
Geeks: 2 for: 1
This data structure stores unique elements in sorted order. It uses the concept of red-black-tree in the background to prevent duplicates.
// Java program to count frequencies of elements // using HashMap. import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.*; class GFG { public static void countFrequencies(ArrayList<String> list) { TreeMap<String, Integer> tmap = new TreeMap<String, Integer>(); for (String t : list) { Integer c = tmap.get(t); tmap.put(t, (c == null) ? 1 : c + 1); } for (Map.Entry m : tmap.entrySet()) System.out.println("Frequency of " + m.getKey() + " is " + m.getValue()); } public static void main(String[] args) { ArrayList<String> list = new ArrayList<String>(); list.add("Geeks"); list.add("for"); list.add("Geeks"); countFrequencies(list); } } |
Frequency of Geeks is 2 Frequency of for is 1
Key Points:
- HashMap implements Map Interface while TreeMap implements SortedMap Interface.
- HashMap uses Hashing whereas TreeMap uses Red-Black Tree(Balanced BST). So HashMap based solutions are generally much faster than TreeMap based solutions.
Recommended Posts:
- Count occurrences of a given character using Regex in Java
- Java program to count the occurrences of each character
- Count minimum frequency elements in a linked list
- Remove elements from a List that satisfy given predicate in Java
- Pattern Occurrences : Stack Implementation Java
- Remove all occurrences of an element from Array in Java
- Count occurrences of a string that can be constructed from another given string
- Program to convert List of String to List of Integer in Java
- Program to convert List of Integer to List of String in Java
- Count elements present in first array but not in second
- Count all elements in the array which appears at least K times after their first occurrence
- Count frequencies of all elements in array in Python using collections module
- Find the difference of count of equal elements on the right and the left for each element
- Set to List in Java
- List to Set in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : mike00



