Related Articles
Count occurrences of elements of list in Java
• Last Updated : 11 Dec, 2018

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 list)``    ``{``        ``// hashmap to store the frequency of element``        ``Map hm = ``new` `HashMap();`` ` `        ``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 val : hm.entrySet()) {``            ``System.out.println(``"Element "` `+ val.getKey() + ``" "``                               ``+ ``"occurs"``                               ``+ ``": "` `+ val.getValue() + ``" times"``);``        ``}``    ``}`` ` `    ``public` `static` `void` `main(String[] args)``    ``{``        ``ArrayList list = ``new` `ArrayList();``        ``list.add(``"Geeks"``);``        ``list.add(``"for"``);``        ``list.add(``"Geeks"``);``        ``countFrequencies(list);``    ``}``}`
Output:
```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 list)``    ``{`` ` `        ``// hash set is created and elements of``        ``// arraylist are insertd into it``        ``Set st = ``new` `HashSet(list);``        ``for` `(String s : st)``            ``System.out.println(s + ``": "` `+ Collections.frequency(list, s));``    ``}`` ` `    ``public` `static` `void` `main(String[] args)``    ``{``        ``ArrayList list = ``new` `ArrayList();``        ``list.add(``"Geeks"``);``        ``list.add(``"for"``);``        ``list.add(``"Geeks"``);``        ``countFrequencies(list);``    ``}``}`
Output:
```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 list)``    ``{`` ` `        ``TreeMap tmap = ``new` `TreeMap();``        ``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 list = ``new` `ArrayList();``        ``list.add(``"Geeks"``);``        ``list.add(``"for"``);``        ``list.add(``"Geeks"``);``        ``countFrequencies(list);``    ``}``}`
Output:
```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.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up