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.
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 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.
- Count occurrences of elements of list in Java
- Java.util.Collections.disjoint() Method in java with Examples
- Java tricks for competitive programming (for Java 8)
- Java.util.Arrays.equals() in Java with Examples
- Java.util.Collections.rotate() Method in Java with Examples
- Java.util.Collections.frequency() in Java
- Double Brace Initialization in Java
- Collections.reverseOrder() in Java with Examples
- Collections.shuffle() in Java with Examples
- Collections.binarySearch() in Java with Examples
- ArrayList in Java
- Null Pointer Exception In Java
- HashSet in Java
- Collections in Java
- Stack Class in Java