Java.util.Collections.frequency() in Java with Examples
java.util.Collections.frequency() method is present in java.util.Collections class. It is used to get the frequency of a element present in the specified list of Collection. More formally, it returns the number of elements e in the collection.
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.
public static int frequency(Collection<?> c, Object o) Parameters : c - the collection in which to determine the frequency of o o - the object whose frequency is to be determined Returns : Returns the number of elements in the specified collection equal to the specified object. Throws: NullPointerException - if c is null
How to Quickly get frequency of an element in an array in Java ?
Arrays class in Java doesn’t have frequency method. But we can use Collections.frequency() to get frequency of an element in an array also.
This article is contributed by Gaurav Miglani. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.