🎓 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
ArrayList.ensureCapacity() method in Java is used to increase the capacity of the ArrayList to ensure it can hold a specified number of elements without the need for resizing. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.Table of Contents
- Introduction
ensureCapacityMethod Syntax- Examples
- Increasing the Capacity of an ArrayList
- Pre-allocating Capacity for Performance Optimization
- Real-World Use Case
- Conclusion
Introduction
The ArrayList.ensureCapacity() method is part of the ArrayList class in Java. It is used to increase the capacity of the ArrayList to accommodate a specified number of elements, which can help to avoid multiple resizings when adding a large number of elements.
ensureCapacity Method Syntax
The syntax for the ensureCapacity method is as follows:
public void ensureCapacity(int minCapacity)
- minCapacity: The desired minimum capacity of the
ArrayList.
Examples
Increasing the Capacity of an ArrayList
The ensureCapacity method can be used to ensure that the ArrayList can hold a specified number of elements without resizing.
Example
import java.util.ArrayList;
import java.util.List;
public class EnsureCapacityExample {
public static void main(String[] args) {
List<String> list = new ArrayList<>(5);
list.add("Apple");
list.add("Banana");
list.add("Orange");
// Ensure the capacity is at least 10
((ArrayList<String>) list).ensureCapacity(10);
list.add("Grapes");
list.add("Pineapple");
System.out.println("ArrayList after ensureCapacity: " + list);
}
}
Output:
ArrayList after ensureCapacity: [Apple, Banana, Orange, Grapes, Pineapple]
Pre-allocating Capacity for Performance Optimization
Using ensureCapacity can be beneficial in scenarios where you know in advance the number of elements you will be adding to the list, as it reduces the need for resizing operations and improves performance.
Example
import java.util.ArrayList;
import java.util.List;
public class PreAllocateCapacityExample {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
// Pre-allocate capacity for 100 elements
((ArrayList<String>) list).ensureCapacity(100);
for (int i = 1; i <= 100; i++) {
list.add("Element " + i);
}
System.out.println("ArrayList after adding 100 elements: " + list.size());
}
}
Output:
ArrayList after adding 100 elements: 100
Real-World Use Case
Batch Processing
In a batch processing scenario, where you need to process and store a large number of items, using ensureCapacity can improve performance by reducing the number of resizing operations.
Example
import java.util.ArrayList;
import java.util.List;
public class BatchProcessing {
public static void main(String[] args) {
List<String> batchData = new ArrayList<>();
// Pre-allocate capacity for 1000 elements
((ArrayList<String>) batchData).ensureCapacity(1000);
// Simulate batch processing
for (int i = 1; i <= 1000; i++) {
batchData.add("Record " + i);
}
System.out.println("Batch data size: " + batchData.size());
}
}
Output:
Batch data size: 1000
Conclusion
The ArrayList.ensureCapacity() method in Java is a useful tool for optimizing the performance of your ArrayList by pre-allocating space for a specified number of elements. By understanding how to use this method, you can efficiently manage the capacity of your lists and avoid the overhead of multiple resizing operations. Whether you are handling batch processing or large datasets, the ensureCapacity method provides a straightforward and effective solution for improving performance.
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