12:17 am - Monday June 26, 2017

Collection.frequency returns the number of elements in the specified collection frequency(Collection c, Object o)

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

BACK

Collection.frequency returns the number of elements in the specified collection  frequency(Collection<?> c, Object o)

Example Program


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

public class Frequency {
	public static void main(String[] args) {
		ArrayList<String> al = new ArrayList<String>();
		al.add("A");
		al.add("B");
		al.add("C");
		al.add("C");
		al.add("C");
		al.add("C");
		int freq = Collections.frequency(al, "C");
		System.out.println("Frequency of B is:" + freq);

	}

}

 

OUTPUT:

Frequency of B is:4

 

EXPLANATION:

Collection.frequency returns the number of elements in the specified collection equal to the specified object. It basically equaling obj2 with List objects.

If you override the equals method and put your logic like based on score attribute Object will be equals or not. Then Collection.frequency return your desired result

PARAMETER:

 

  • c–This is the collection in which to determine the frequency of o.
  • o–This is the object whose freque

 

RETURN VALUE:

NullPointerException –This is thrown if c is null.

 

EXCEPTION:

 

  • NullPointerException –This is thrown if c is null.

 

 

BACK

 

Filed in: Z-A All Java Codes

Comments are closed.