🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this guide, you will learn about the Collections frequency() method in Java programming and how to use it with an example.
1. Collections frequency() Method Overview
Definition:
The frequency() method of the Collections class in Java is used to get the number of occurrences of a specified element in a collection.
Syntax:
Collections.frequency(Collection<?> c, Object o)
Parameters:
- c: The collection in which to determine the frequency of o.
- o: The element whose frequency is to be determined.
Key Points:
- If the collection contains multiple instances of the specified element, this method counts all of them.
- If the specified collection is null or if the specified element is null and the collection doesn't permit null elements, the method will throw a NullPointerException.
- The method uses the equals method to check for the presence of the element.
- This is a simple method to quickly check the frequency of an item in a collection without needing to iterate through the collection manually.
2. Collections frequency() Method Example
import java.util.*;
public class CollectionsFrequencyExample {
public static void main(String[] args) {
List<String> fruits = new ArrayList<>(Arrays.asList("apple", "banana", "apple", "orange", "banana", "apple"));
// Counting occurrences of 'apple'
int appleCount = Collections.frequency(fruits, "apple");
System.out.println("Occurrences of apple: " + appleCount);
// Counting occurrences of 'mango' which is not in the list
int mangoCount = Collections.frequency(fruits, "mango");
System.out.println("Occurrences of mango: " + mangoCount);
}
}
Output:
Occurrences of apple: 3 Occurrences of mango: 0
Explanation:
In the example, we have a list of fruits with repeated occurrences of "apple" and "banana".
We utilize the frequency() method to determine the count of "apple" in the list, which is 3.
We then attempt to determine the frequency of "mango", which is not present in the list. As a result, the method returns 0, indicating that "mango" does not appear in the list.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment