Increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.
Examples
package com.logicbig.example.arraylist;
import java.util.*;
public class EnsureCapacityExample {
public static void main(String... args) { // Pre-allocate capacity before adding many elements ArrayList<String> list = new ArrayList<>(); list.ensureCapacity(5000); for(int i = 0; i < 5000; i++) { list.add("Data" + i); } System.out.println("Added 5000 elements without resizing: " + list.size()); } }