Java.util.Collections.frequency() in Java - GeeksforGeeks

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 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 program to demonstrate working of // Collections.frequency()// for custom defined objectsimport 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

This article is contributed by Tanisha Mittal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Java - util package
Java-Collections
Java-Collections-Class
Java-Functions
Java
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Object Oriented Programming (OOPs) Concept in Java
HashMap in Java with Examples
Stream In Java
Interfaces in Java
How to iterate any Map in Java
Initialize an ArrayList in Java
ArrayList in Java
Stack Class in Java
Singleton Class in Java
Multidimensional Arrays in Java