Collections static int frequency(Collection c,Object o) method Example Program


Returns the number of elements in the specified collection equal to the specified object. More formally, returns the number of elements e in the collection such that (o == null ? e == null : o.equals(e)).

Program

package com.candidjava;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 
 * @author karthikeyan.T
 * @description the following code shows how to use
 *              Collections.frequency(Collection <?> c, Object o) method.
 */
public class Frequency {
	public static void main(String args[]) {
		List<String> arrlist = new ArrayList<String>();
		arrlist.add("karthik");
		arrlist.add("kamal");
		arrlist.add("mohan");
		arrlist.add("mohan");
		arrlist.add("hari");
		arrlist.add("anand");
		int freq = Collections.frequency(arrlist, "mohan");
		System.out.println("Frequency of 'mohan' is: " + freq);
	}
}

Output

Frequency of 'mohan' is: 2

Explanation

public static int frequency(Collection<?> c,Object o)
Returns the number of elements in the specified collection equal to the specified object. More formally, returns the number of elements e in the collection such that (o == null ? e == null : o.equals(e)).
Parameters:
c - the collection in which to determine the frequency of o
o - the object whose frequency is to be determined
Throws:
NullPointerException - if c is null
Since:
1.5


Related Post

Comments


©candidjava.com