Understanding Collections.frequency() Method in Java

Understanding Collections.frequency() Method in Java


If you are working with collections in Java, then you must have come across the Collections class in java.util package. It is a utility class that provides a set of useful methods to work with collections. One of these methods is the Collections.frequency() method that returns the number of occurrences of an element in the specified collection. In this article, we will explore the basics of the Collections.frequency() method in Java with examples.

Syntax:

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

Parameters:

  • c - The collection in which to determine the frequency of o.
  • o - The object whose frequency is to be determined.

Return Value: Returns the number of elements in the specified collection equal to the specified object.

Let's see some examples to understand how to use the Collections.frequency() method.

Example 1: Count the frequency of an element in a list

In this example, we will create an ArrayList of integers and count the frequency of the integer 10.

csharp
import java.util.*; public class Example1 { public static void main(String[] args) { List<Integer> list = new ArrayList<Integer>(); list.add(10); list.add(20); list.add(10); int frequency = Collections.frequency(list, 10); System.out.println(frequency); // Output: 2 } }

Example 2: Count the frequency of a user-defined object in a list

In this example, we will create a list of Point objects and count the frequency of a Point object (10, 20) in the list. Initially, we will not get the desired output because the default equals() method does not work with user-defined objects. To get the correct output, we will override the equals() method of the Point class.

java
import java.util.*; class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } @Override public boolean equals(Object o) { if (o == this) return true; if (!(o instanceof Point)) return false; Point p = (Point)o; return p.x == x && p.y == y; } } public class Example2 { public static void main(String[] args) { List<Point> list = new ArrayList<Point>(); list.add(new Point(10, 20)); list.add(new Point(5, 25)); list.add(new Point(10, 20)); Point p = new Point(10, 20); int frequency = Collections.frequency(list, p); System.out.println(frequency); // Output: 2 } }

Example 3: Count the frequency of an element in an array

In this example, we will count the frequency of the integer 10 in an array of integers. We will use the Arrays.asList() method to convert the array to a list.

java
import java.util.*; public class Example3 { public static void main(String[] args) { Integer[] arr = {10, 15, 10, 20}; int frequency = Collections.frequency(Arrays.asList(arr), 10); System.out.println(frequency); // Output: 2 } }

Conclusion

The Collections.frequency() method is a useful utility method provided by the Collections class in Java to count the frequency of an element in a collection. It is a simple and efficient way to find the number of occurrences of an element in a collection.

Previous Post Next Post