The method is a java.util.Collections class method. It counts the frequency of the specified element in the given list. It override the equals() method to perform the comparison to check if the specified Object and the Object in the list are equal or not.
public static int frequency(Collection c, Object o) parameters: c: Collection in which to determine the frequency of o. o: Object whose frequency is to be determined. It throws Null Pointer Exception if the Collection c is null.
The frequency of the word code is: 3
Using Java.util. Collections.frequency() for Custom defined objects
The method stated above works well for already defined Objects in java, but what about the custom defined objects. Well, to count the frequency of a custom defined object in java, we will have to simply override the equals() method. Lets see how we can do that.
The frequency of the Student Ram,19 is: 2
This article is contributed by Tanisha Mittal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java.util.function.DoublePredicate interface in Java with Examples
- Java.util.Collections.rotate() Method in Java with Examples
- Java.util.function.BiPredicate interface in Java with Examples
- Java.util.function.LongPredicate interface in Java with Examples
- Java.util.function.IntPredicate interface in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
- Java.util.concurrent.RecursiveAction class in Java with Examples
- Java Deprecated API Scanner tool (jdepscan) in Java 9 with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java.util.concurrent.Phaser class in Java with Examples
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java.lang.Short toString() method in Java with Examples
- Java.util.LinkedList.peek() , peekfirst(), peeklast() in Java
- Java.util.LinkedList.offer(), offerFirst(), offerLast() in Java