Java HashSet newHashSet() Method (Introduced in Java 21)

The 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

  1. Introduction
  2. newHashSet Method Syntax
  3. Examples
    • Basic Example
    • Real-World Use Case: Pre-sizing a HashSet for Usernames
  4. 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 HashSet will hold.
  • Returns: A new HashSet instance.

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.

Comments