DEV Community

realNameHidden
realNameHidden

Posted on

Collections frequency method in java | Collections framework | Java

The Collections.frequency method in Java is a static method provided by the java.util.Collections class. It is used to count the number of occurrences of a specified element in a given collection.

public static int frequency(Collection<?> c, Object o)

Enter fullscreen mode Exit fullscreen mode

Parameters
c: The collection in which to count occurrences of the specified element.

o: The element whose frequency is to be counted.
Return Value

int: The number of occurrences of the specified element in the collection.

Example Usage
Let’s look at some examples demonstrating how to use the Collections.frequency method.

Example 1: Counting Occurrences in a List

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

public class Main {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("apple");
        list.add("banana");
        list.add("apple");
        list.add("orange");
        list.add("apple");

        int frequency = Collections.frequency(list, "apple");
        System.out.println("Frequency of 'apple': " + frequency); // Output: 3
    }
}

Enter fullscreen mode Exit fullscreen mode

output : 3

Example 2: Counting Occurrences in a Set
Since sets do not allow duplicate elements, the frequency of any element in a set will be either 0 or 1.

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class Main {
    public static void main(String[] args) {
        Set<String> set = new HashSet<>();
        set.add("apple");
        set.add("banana");
        set.add("orange");

        int frequency = Collections.frequency(set, "apple");
        System.out.println("Frequency of 'apple': " + frequency); // Output: 1

        frequency = Collections.frequency(set, "pear");
        System.out.println("Frequency of 'pear': " + frequency); // Output: 0
    }
}

Enter fullscreen mode Exit fullscreen mode

Example 3: Counting Occurrences in a List of Integers

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

public class Main {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(1);
        list.add(3);
        list.add(1);

        int frequency = Collections.frequency(list, 1);
        System.out.println("Frequency of 1: " + frequency); // Output: 3
    }
}

Enter fullscreen mode Exit fullscreen mode

Example 4: Counting Occurrences in a Custom Object Collection
When using custom objects, ensure the equals method is properly overridden to count occurrences correctly.

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

class Person {
    String name;
    int age;

    Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return age == person.age && Objects.equals(name, person.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}

public class Main {
    public static void main(String[] args) {
        List<Person> list = new ArrayList<>();
        list.add(new Person("Alice", 30));
        list.add(new Person("Bob", 25));
        list.add(new Person("Alice", 30));
        list.add(new Person("Charlie", 35));

        int frequency = Collections.frequency(list, new Person("Alice", 30));
        System.out.println("Frequency of Alice (30): " + frequency); // Output: 2
    }
}

Enter fullscreen mode Exit fullscreen mode

Summary
The Collections.frequency method is a convenient way to count the number of times a specific element appears in a collection. It can be used with any type of collection, including lists and sets, and works with custom objects as long as their equals method is correctly overridden. This method is useful for quick frequency analysis in a variety of collection types.

Top comments (0)