90
Java Collections frequency() Method
The frequency() method of Java Collections class is used to get the number of elements in the specified collection equal to the specified object.
Syntax
Following is the declaration of frequency() method:
Parameter
Parameter | Description | Required/Optional |
---|---|---|
c | It is the collection in which to determine the frequency of obj. | Required |
obj | It is the object whose frequency is to be determined. | Required |
Returns
The frequency() method returns the number of elements in the collections c equal to the specified object obj.
Exceptions
NullPointerException– This exception will be thrown if collection c is null.
Compatibility Version
Java 1.5 and above
Example 1
Output:
List of elements: [Java, COBOL, Java, C++, Java] Frequency of the Word: 3
Example 2
Output:
List of elements: [1, 2, 1, 3, 2, 3, 4] Count all with frequency: 1: 2 2: 2 3: 2 4: 1
Example 3
Output:
Frequency of 20 is: 4
Next TopicJava Collections Class