The frequency() method in Java is a utility method that returns the number of occurrences of a specified element in a given collection. This method is part of the java.util.Collections class and provides a convenient way to count the frequency of elements within a collection.
Table of Contents
- Introduction
frequency()Method Syntax- Examples
- Basic Usage of
frequency() - Using
frequency()with Custom Classes
- Basic Usage of
- Real-World Use Case
- Conclusion
Introduction
The Collections.frequency() method is used to determine how many times a specified element appears in a given collection. This method is useful when you need to analyze the content of a collection and count the occurrences of specific elements.
The method uses the equals() method to compare the specified element with the elements in the collection, ensuring that the comparison is consistent with the collection’s element type.
frequency() Method Syntax
The syntax for the frequency() method is as follows:
public static int frequency(Collection<?> c, Object o)
Parameters:
c: The collection in which to count the occurrences of the specified element.o: The element whose frequency is to be counted.
Returns:
- The number of occurrences of the specified element in the specified collection.
Throws:
NullPointerExceptionif the specified collection is null.
Examples
Basic Usage of frequency()
The following example demonstrates how to use the frequency() method to count the occurrences of a specified element in a list.
Example
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class FrequencyExample {
public static void main(String[] args) {
// Create a list with initial elements
List<String> fruits = new ArrayList<>();
Collections.addAll(fruits, "Apple", "Banana", "Apple", "Cherry", "Apple", "Banana");
// Display the list
System.out.println("Fruit List: " + fruits);
// Use the frequency() method to count occurrences of "Apple"
int appleCount = Collections.frequency(fruits, "Apple");
// Display the frequency of "Apple"
System.out.println("Frequency of 'Apple': " + appleCount);
}
}
Output:
Fruit List: [Apple, Banana, Apple, Cherry, Apple, Banana]
Frequency of 'Apple': 3
Using frequency() with Custom Classes
You can also use the frequency() method with collections containing instances of custom classes.
Example
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class Student {
String name;
Student(String name) {
this.name = name;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Student student = (Student) obj;
return name.equals(student.name);
}
@Override
public int hashCode() {
return name.hashCode();
}
@Override
public String toString() {
return name;
}
}
public class CustomFrequencyExample {
public static void main(String[] args) {
// Create a list of students
List<Student> students = new ArrayList<>();
students.add(new Student("Amit"));
students.add(new Student("Neha"));
students.add(new Student("Amit"));
students.add(new Student("Raj"));
students.add(new Student("Amit"));
// Display the student list
System.out.println("Student List: " + students);
// Use the frequency() method to count occurrences of "Amit"
int amitCount = Collections.frequency(students, new Student("Amit"));
// Display the frequency of "Amit"
System.out.println("Frequency of 'Amit': " + amitCount);
}
}
Output:
Student List: [Amit, Neha, Amit, Raj, Amit]
Frequency of 'Amit': 3
Explanation:
-
Original List: The initial list contains multiple instances of the
Studentclass with the name "Amit". -
Frequency Count: The
frequency()method is used to count the occurrences of "Amit" in the list, demonstrating its utility with custom classes.
Real-World Use Case
Analyzing Word Frequencies in a Text
In real-world applications, the frequency() method can be used to analyze the frequency of words in a text. This is particularly useful in scenarios such as text analysis, natural language processing, and creating word frequency distributions.
Example
Imagine a scenario where you need to count the frequency of words in a list of sentences.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class WordFrequencyExample {
public static void main(String[] args) {
// Create a list of words from a text
List<String> words = new ArrayList<>();
Collections.addAll(words, "hello", "world", "hello", "java", "world", "hello");
// Display the list of words
System.out.println("Word List: " + words);
// Use the frequency() method to count occurrences of "hello"
int helloCount = Collections.frequency(words, "hello");
// Display the frequency of "hello"
System.out.println("Frequency of 'hello': " + helloCount);
// Use the frequency() method to count occurrences of "world"
int worldCount = Collections.frequency(words, "world");
// Display the frequency of "world"
System.out.println("Frequency of 'world': " + worldCount);
}
}
Output:
Word List: [hello, world, hello, java, world, hello]
Frequency of 'hello': 3
Frequency of 'world': 2
Explanation:
-
Word List: The list contains words from a text.
-
Frequency Count: The
frequency()method is used to count the occurrences of "hello" and "world" in the list, demonstrating its utility in text analysis.
Conclusion
The Collections.frequency() method is a powerful utility for counting the occurrences of a specified element in a collection in Java. By providing a simple way to analyze and count elements, it enhances the flexibility and readability of your code. This method is particularly valuable in scenarios where you need to determine the frequency of elements in collections, improving the robustness and maintainability of your Java applications.