Javatpoint Logo
Javatpoint Logo

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

Test it Now

Output:

List of elements: [Java, COBOL, Java, C++, Java]
Frequency of the Word: 3

Example 2

Test it Now

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

Test it Now

Output:

Frequency of 20 is: 4





Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA