java.util.Collections.frequency() Method

Advertisements


Description

The frequency(Collection<?>, Object) method is used to get the number of elements in the specified collection equal to the specified object.

Declaration

Following is the declaration for java.util.Collections.frequency() method.

public static int frequency(Collection<?> c, Object o)

Parameters

  • c--This is the collection in which to determine the frequency of o.

  • o--This is the object whose frequency is to be determined.

Return Value

NA

Exception

  • NullPointerException --This is thrown if c is null.

Example

The following example shows the usage of java.util.Collections.frequency()

package com.tutorialspoint;

import java.util.*;

public class CollectionsDemo {
   public static void main(String args[]) {
      // create array list object       
      List arrlist = new ArrayList();
      
      // populate the list
      arrlist.add("A");
      arrlist.add("B");
      arrlist.add("C");
      arrlist.add("C");
      arrlist.add("C");      
           
      // check frequensy of 'C'
      int freq = Collections.frequency(arrlist, "C");
      
      System.out.println("Frequency of 'C' is: "+freq);    
   }    
}

Let us compile and run the above program, this will produce the following result.

Frequency of 'C' is: 3



Advertisements
Advertisements