🎓 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.newHashSet() method, introduced in Java 21, provides a convenient way to create a new, empty HashSet with a specified expected number of elements. Table of Contents
- Introduction
newHashSetMethod Syntax- Examples
- Basic Example
- Real-World Use Case: Pre-sizing a HashSet for Usernames
- 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 newHashSet() method introduced in Java 21 allows for the creation of a HashSet with an expected number of elements, optimizing the internal storage.
newHashSet() Method Syntax
The syntax for the newHashSet method is as follows:
public static <E> HashSet<E> newHashSet(int expectedSize)
- expectedSize: The expected number of elements the
HashSetwill hold. - Returns: A new
HashSetinstance.
Examples
Basic Example
In this example, we'll use the newHashSet method to create a HashSet with an expected size.
Example
import java.util.HashSet;
public class HashSetNewHashSetExample {
public static void main(String[] args) {
// Creating a new HashSet with an expected size of 10
HashSet<String> set = HashSet.newHashSet(10);
// Adding elements to the HashSet
set.add("Java");
set.add("Python");
set.add("C");
set.add("JavaScript");
// Printing the HashSet
System.out.println("HashSet: " + set);
}
}
Output:
HashSet: [Java, JavaScript, Python, C]
Real-World Use Case: Pre-sizing a HashSet for Usernames
In a web application, you might want to pre-size a HashSet for storing usernames, especially when you know the approximate number of users expected.
Example
import java.util.HashSet;
public class PreSizedHashSetExample {
public static void main(String[] args) {
// Creating a new HashSet with an expected size of 100
HashSet<String> usernames = HashSet.newHashSet(100);
// Simulating adding usernames
usernames.add("john_doe");
usernames.add("jane_smith");
usernames.add("alice_jones");
// Printing the HashSet
System.out.println("Usernames: " + usernames);
}
}
Output:
Usernames: [john_doe, jane_smith, alice_jones]
Example: Pre-sizing a HashSet for Collecting Unique Error Codes
In a logging system, you might want to pre-size a HashSet for collecting unique error codes, especially if you know the approximate number of different error codes expected.
Example
import java.util.HashSet;
public class ErrorCodesExample {
public static void main(String[] args) {
// Creating a new HashSet with an expected size of 50
HashSet<Integer> errorCodes = HashSet.newHashSet(50);
// Simulating adding error codes
errorCodes.add(404);
errorCodes.add(500);
errorCodes.add(403);
// Printing the HashSet
System.out.println("Error Codes: " + errorCodes);
}
}
Output:
Error Codes: [404, 500, 403]
Example: Pre-sizing a HashSet for Tracking Unique IP Addresses
In a network monitoring application, you might want to pre-size a HashSet for tracking unique IP addresses, especially if you know the approximate number of IP addresses expected.
Example
import java.util.HashSet;
public class UniqueIPAddressesExample {
public static void main(String[] args) {
// Creating a new HashSet with an expected size of 200
HashSet<String> ipAddresses = HashSet.newHashSet(200);
// Simulating adding IP addresses
ipAddresses.add("192.168.1.1");
ipAddresses.add("192.168.1.2");
ipAddresses.add("192.168.1.3");
// Printing the HashSet
System.out.println("Unique IP Addresses: " + ipAddresses);
}
}
Output:
Unique IP Addresses: [192.168.1.1, 192.168.1.2, 192.168.1.3]
Conclusion
The HashSet.newHashSet() method introduced in Java 21 provides a convenient way to create a HashSet with an expected number of elements. This can optimize the internal storage of the HashSet and improve performance when you have a good estimate of the number of elements that will be added. By understanding how to use this method, you can efficiently manage collections in your Java applications.
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