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.
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
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.
Frequency of Geeks is 2 Frequency of for is 1
- 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 and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Java program to count the occurrences of each character
- Count occurrences of a given character using Regex in Java
- Count Occurrences of Anagrams
- Count occurrences of a string that can be constructed from another given string
- Printing frequency of each character just after its consecutive occurrences
- Replace all occurrences of a string with space
- Check if a binary string has two consecutive occurrences of one everywhere
- Remove all occurrences of any element for maximum array sum
- Map every character of one string to another such that all occurrences are mapped to the same character
- Find the number of occurrences of a character upto preceding position
- Swap all occurrences of two characters to get lexicographically smallest string
- Two player game in which a player can remove all occurrences of a number
- Array sum after replacing all occurrences of X by Y for Q queries
- Sum of all distances between occurrences of same characters in a given string
- Count minimum frequency elements in a linked list
- Count of elements not divisible by any other elements of Array
- Count of Array elements greater than or equal to twice the Median of K trailing Array elements
- Count array elements that can be represented as sum of at least two consecutive array elements
- Program to convert List of Integer to List of String in Java
- Program to convert List of String to List of Integer 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 firstname.lastname@example.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