Java Collection Tutorial - Java Collections .frequency ( Collection <?> c, Object o)








Syntax

Collections.frequency(Collection <?> c, Object o) has the following syntax.

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

Example

In the following code shows how to use Collections.frequency(Collection <?> c, Object o) method.

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/*w  w  w.j  ava 2  s .  c  o  m*/
public class Main {
   public static void main(String args[]) {
      // create array list object       
      List<String> arrlist = new ArrayList<String>();
      
      // populate the list
      arrlist.add("A");
      arrlist.add("B");
      arrlist.add("B");
      arrlist.add("B");
      arrlist.add("B");
      arrlist.add("from java2s.com");
      
      // check frequensy of 'B'
      int freq = Collections.frequency(arrlist, "B");
      
      System.out.println("Frequency of 'B' is: "+freq);    
   }    
}

The code above generates the following result.