Collections.frequency in Java

By Arvind Rai, February 01, 2022
On this page we will learn Java Collections.frequency method.
Find the Java doc.
static int frequency(Collection<?> c, Object o) 
1. Returns the number of elements (frequency) in the specified collection equal to the specified object.
2. Same elements in the collection are found using Objects.equals(o, e) method.
3. The frequency method is a static method and hence we can call by class directly.
4. If specified collection is null, it throws NullPointerException.
5. The frequency method is introduced in java.util.Collections since Java 5.

Example-1

We have a collection of numbers and we are finding the frequency of specified number.
CollectionsFrequency1.java
package com.concretepage;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CollectionsFrequency1 {
  public static void main(String[] args) {
    List<Integer> ageList = new ArrayList<>();
    ageList.add(10);
    ageList.add(20);
    ageList.add(30);
    ageList.add(20);
    
    int freq1 = Collections.frequency(ageList, 20);
    System.out.println(freq1); // Output 2
    
    int freq2 = Collections.frequency(ageList, 30);
    System.out.println(freq2); // Output 1
    
    int freq3 = Collections.frequency(ageList, 50);
    System.out.println(freq3); // Output 0      
 }
} 

Example-2

We have a collection of strings and we are finding the frequency of specified string.
CollectionsFrequency2.java
package com.concretepage;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CollectionsFrequency2 {
  public static void main(String[] args) {
     List<String> cityList = new ArrayList<>();
     cityList.add("Varanasi");
     cityList.add("Noida");
     cityList.add("Noida");
     cityList.add("Prayag");
     
     int freq1 = Collections.frequency(cityList, "Noida");
     System.out.println(freq1); // Output 2
     
     int freq2 = Collections.frequency(cityList, "Varanasi");
     System.out.println(freq2); // Output 1      
  }
} 

Example-3

We have a collection of objects and we are finding the frequency of specified object.
CollectionsFrequency3.java
package com.concretepage;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CollectionsFrequency3 {
    public static void main(String[] args) {
        Person p1 = new Person("Ram");
        Person p2 = new Person("Shyam");
        Person p3 = new Person("Mahesh");
        Person p4 = new Person("Shyam");

        List<Person> list = new ArrayList<Person>();
        list.add(p1);
        list.add(p2);
        list.add(p3);
        list.add(p4);
        
        Person p = new Person("Shyam");
        int freq = Collections.frequency(list, p);
        System.out.println(freq); // Output 2 
    }
}
class Person {
    private String name;
    public Person(String name) {
        this.name = name;
    }
    public String getName() {
	return name;
    }
    @Override
    public boolean equals(Object o) {
        return name.equals(((Person)o).name);
    }
    @Override
    public int hashCode() {
        int hash = 13;
        hash = (31 * hash) + (null == name ? 0 : name.hashCode());
        return hash;
    }
} 

Reference

Java Collections
POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us