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
// 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
// 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 inserted 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
// 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.