🎓 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
HashSet.size() method in Java is used to determine the number of elements in the HashSet. Table of Contents
- Introduction
sizeMethod Syntax- Examples
- Basic Example
- Real-World Use Case: Checking Number of Active Users
- Conclusion
Introduction
The HashSet class in Java is part of the Java Collections Framework and implements the Set interface. A HashSet is used to store unique elements and provides constant-time performance for basic operations like add, remove, contains, and size. The size method is used to determine the number of elements currently in the HashSet.
size() Method Syntax
The syntax for the size method is as follows:
public int size()
- The method does not take any parameters.
- The method returns the number of elements in the
HashSet.
Examples
Basic Example
In this example, we'll use the size method to determine the number of elements in a HashSet.
Example
import java.util.HashSet;
public class HashSetSizeExample {
public static void main(String[] args) {
// Creating a HashSet of Strings
HashSet<String> set = new HashSet<>();
set.add("Java");
set.add("Python");
set.add("C");
set.add("JavaScript");
// Getting the size of the HashSet
int size = set.size();
// Printing the size of the HashSet
System.out.println("Number of elements in the HashSet: " + size);
}
}
Output:
Number of elements in the HashSet: 4
Real-World Use Case: Checking Number of Active Users
In a web application, you might want to check the number of active users currently logged in.
Example
import java.util.HashSet;
public class ActiveUsersSizeExample {
public static void main(String[] args) {
// Creating a HashSet to store active users
HashSet<String> activeUsers = new HashSet<>();
activeUsers.add("john_doe");
activeUsers.add("jane_smith");
activeUsers.add("alice_jones");
// Getting the number of active users
int numberOfActiveUsers = activeUsers.size();
// Printing the number of active users
System.out.println("Number of active users: " + numberOfActiveUsers);
}
}
Output:
Number of active users: 3
Example: Tracking Inventory Items
In an inventory management system, you might want to keep track of the number of unique items in stock.
Example
import java.util.HashSet;
public class InventorySizeExample {
public static void main(String[] args) {
// Creating a HashSet to store inventory items
HashSet<String> inventoryItems = new HashSet<>();
inventoryItems.add("Laptop");
inventoryItems.add("Monitor");
inventoryItems.add("Keyboard");
inventoryItems.add("Mouse");
// Getting the number of inventory items
int numberOfItems = inventoryItems.size();
// Printing the number of inventory items
System.out.println("Number of unique inventory items: " + numberOfItems);
}
}
Output:
Number of unique inventory items: 4
Example: Checking Number of Unique Error Codes
In a logging system, you might want to keep track of the number of unique error codes encountered.
Example
import java.util.HashSet;
public class ErrorCodesSizeExample {
public static void main(String[] args) {
// Creating a HashSet to store unique error codes
HashSet<Integer> errorCodes = new HashSet<>();
errorCodes.add(404);
errorCodes.add(500);
errorCodes.add(403);
// Getting the number of unique error codes
int numberOfErrorCodes = errorCodes.size();
// Printing the number of unique error codes
System.out.println("Number of unique error codes: " + numberOfErrorCodes);
}
}
Output:
Number of unique error codes: 3
Conclusion
The HashSet.size() method in Java provides a way to determine the number of elements in a HashSet. This method is useful in various scenarios, such as managing active users in a web application, tracking inventory items, or counting unique error codes. By understanding how to use this method, you can efficiently manage and manipulate sets in your Java applications.
Comments
Post a Comment
Leave Comment