![]() |
VOOZH | about |
Collections.frequency() method in Java is a utility method provided by the java.util.Collections class. It is used to count how many times a specified element occurs in a given collection.
Example:
The frequency of the word code is: 3
Explanation:
public static int frequency(Collection<?> c, Object o)
Parameters:
Return Value: Returns an int representing the number of times the specified object appears in the collection.
For custom-defined objects, Collections.frequency() works only if the equals() method is properly overridden. This ensures that two objects are compared based on their content rather than their memory reference.
Example: This code demonstrates how to use Collections.frequency() to count how many times a specific element appears in a list.
The frequency of the Student Ram, 19 is: 2
Explanation: