Open In App

Methods to Create Preallocated HashMap in Java 19

Last Updated : 08 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Java 19 has introduced some new methods to create preallocated HashMaps which can enhance the performance of your application. In this article, we will explore the concept of preallocated HashMaps and how to create them using the new methods in Java 19.

HashMap is a widely used data structure in Java for storing key-value pairs. However, when you create a new HashMap, it is created with an initial capacity of 16 and if the number of elements exceeds this capacity, the HashMap needs to be resized which can be a time-consuming process. To avoid this, you can create preallocated HashMaps with a fixed initial capacity that matches the expected number of elements.

Implementation

Creating preallocated HashMaps in Java 19 can be done in two ways:

Method 1: Using the new constructor with a specified initial capacity:

Map<Integer, String> preallocatedHashMap = new HashMap<>(10);

Method 2: Using the new method Map.ofEntries() with a specified initial capacity:

Map<Integer, String> preallocatedHashMap = Map.ofEntries(
  Map.entry(1, "one"),
  Map.entry(2, "two"),
  Map.entry(3, "three")
);

Let’s take a closer look at each of these methods and their implementation.

Method 1: Using the new constructor with a specified initial capacity

The new constructor introduced in Java 19 allows you to create a new HashMap with a specified initial capacity. The initial capacity represents the number of buckets in the HashMap, and it is recommended to set it to the expected number of elements to avoid resizing.

Here’s an example of how to create a preallocated HashMap using the new constructor:

Map<Integer, String> preallocatedHashMap = new HashMap<>(10);
preallocatedHashMap.put(1, "one");
preallocatedHashMap.put(2, "two");
preallocatedHashMap.put(3, "three");

In this example, we create a new HashMap with an initial capacity of 10 using the new constructor. We then add three key-value pairs to the HashMap.

Method 2: Using the new method Map.ofEntries() with a specified initial capacity

The new method Map.ofEntries() introduced in Java 19 allows you to create a new HashMap with a specified initial capacity and add key-value pairs to it in one line of code. Here’s an example of how to create a preallocated HashMap using Map.ofEntries():

Map<Integer, String> preallocatedHashMap = Map.ofEntries(
  Map.entry(1, "one"),
  Map.entry(2, "two"),
  Map.entry(3, "three")
);

In this example, we create a new HashMap with an initial capacity of 3 using Map.ofEntries(). We then add three key-value pairs to the HashMap using the Map.entry() method.

Note: It is important to note that the Map.ofEntries() method returns an unmodifiable map, which means that you cannot add or remove key-value pairs from the map. If you need to modify the map later on, you should use the new constructor with a specified initial capacity.

Conclusion

In conclusion, creating preallocated HashMaps in Java 19 can greatly improve the performance of your application. By specifying the initial capacity of the HashMap to match the expected number of elements, you can avoid resizing and reduce the time taken to access and manipulate the data. The two methods introduced in this article provide easy and efficient ways to create preallocated HashMaps in Java 19.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads