Java.util.Collections.frequency() in Java
Last Updated :
07 Dec, 2018
The method is a java.util.Collections class method. It counts the frequency of the specified element in the given list. It
override the equals() method to perform the comparison to check if the specified Object and the Object in the list are equal or not.
Syntax:
public static int frequency(Collection c, Object o)
parameters:
c: Collection in which to determine the frequency of o.
o: Object whose frequency is to be determined.
It throws Null Pointer Exception if the Collection c is null.
Java
// Java program to demonstrate
// working of Collections.frequency()
import java.util.*;
public class GFG
{
public static void main(String[] args)
{
// Let us create a list with 4 items
ArrayList<String> list =
new ArrayList<String>();
list.add("code");
list.add("code");
list.add("quiz");
list.add("code");
// count the frequency of the word "code"
System.out.println("The frequency of the word code is: "+
Collections.frequency(list, "code"));
}
}
Output:
The frequency of the word code is: 3
Using Java.util. Collections.frequency() for Custom defined objects
The method stated above works well for already defined Objects in java, but what about the custom defined objects. Well, to count the frequency of a custom defined object in java, we will have to simply override the equals() method. Lets see how we can do that.
Java
// Java program to demonstrate working of
// Collections.frequency()
// for custom defined objects
import java.util.*;
public class GFG
{
public static void main(String[] args)
{
// Let us create a list of Student type
ArrayList<Student> list =
new ArrayList<Student>();
list.add(new Student("Ram", 19));
list.add(new Student("Ashok", 20));
list.add(new Student("Ram", 19));
list.add(new Student("Ashok", 19));
// count the frequency of the word "code"
System.out.println("The frequency of the Student Ram, 19 is: "+
Collections.frequency(list, new Student("Ram", 19)));
}
}
class Student
{
private String name;
private int age;
Student(String name, int age)
{
this.name=name;
this.age=age;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
@Override
public boolean equals(Object o)
{
Student s;
if(!(o instanceof Student))
{
return false;
}
else
{
s=(Student)o;
if(this.name.equals(s.getName()) && this.age== s.getAge())
{
return true;
}
}
return false;
}
}
Output:
The frequency of the Student Ram,19 is: 2
Similar Reads
Java.util.Collections.frequency() in Java with Examples java.util.Collections.frequency() method is present in java.util.Collections class. It is used to get the frequency of a element present in the specified list of Collection. More formally, it returns the number of elements e in the collection. Syntax public static int frequency(Collection<?> c
2 min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Need of Concurrent Collections in java In Java, multiple threads can work at the same time, but when the applications become complex, it is very important to handle multiple threads at the same time. With the help of concurrency, different tasks can run in parallel, which can improve the performance of the application. Managing shared da
3 min read
Collection Interface in Java The Collection interface in Java is a core member of the Java Collections Framework located in the java.util package. It is one of the root interfaces of the Java Collection Hierarchy. The Collection interface is not directly implemented by any class. Instead, it is implemented indirectly through it
6 min read
Java Collection Tutorial Java Collection Framework is unlikely any group of individual objects which are represented as a single unit be it of any type is known as the collection of objects. Earlier in Java, there was no such thing defined which holds true for it so there arises a need in the next versions of any such conce
15+ min read