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 lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java.util.LinkedList.offer(), offerFirst(), offerLast() in Java
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java.util.LinkedList.peek() , peekfirst(), peeklast() in Java
- Java lang.Long.byteValue() method in Java with Examples
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java lang.Long.reverse() method in Java with Examples
- Java.util.Collections.rotate() Method in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
- Java Clock withZone() method in Java with Examples
- Java Clock tickMinutes() method in Java with Examples
- Java Swing | Translucent and shaped Window in Java