How to Initialize a Vector with a Specific Initial Capacity in Java? Last Updated : 12 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In Java, Vector Class allows to creation of dynamic arrays that can grow or shrink as per the need. If we know the approximate size of elements, we will store this in the vector. Then we optimize memory usage, and we can initialize it with the specific initial capacity. In this article, we will learn how to initialize a Vector with a specific initial capacity in Java. Example Input-Output:Input: int initialCapacity = 10;Vector<String> string Vector = new Vector<>(initialCapacity); Output: A Vector named stringVector is created with the initial capacity of 10. Syntax:Vector<E> vector = new Vector<>(initialCapacity);E: The type of elements in Vector.initialCapacity: The initial capacity of the Vector. It represents the number of elements the Vector can initially store without the resizing.Program to Initialize a Vector with Initial Capacity in JavaBelow is the implementation to Initialize a Vector with Initial Capacity: Java // Java program to initialize a // Vector with a specific initial capacity import java.io.*; import java.util.Vector; // Driver Class public class GFG { // Main Function public static void main(String[] args) { // Initializing a Vector with // the specific initial capacity int Capacity = 10; Vector<String> stringVector = new Vector<>(Capacity); // Adding elements to Vector stringVector.add("Java"); stringVector.add("is"); stringVector.add("powerful."); // Displaying the elements of the Vector System.out.println("The Vector elements: " + stringVector); } } OutputThe Vector elements: [Java, is, powerful.] Explanation of the Program:In the above program, Initialization: The initial capacity is set to 10. A Vector named stringVector is created with this initial capacity.Adding Elements: Three string elements are added to stringVector.Displaying Elements: The elements of stringVector are printed to console. Comment More infoAdvertise with us Next Article How to Initialize a Vector with a Specific Initial Capacity in Java? M maha123 Follow Improve Article Tags : Java Java Programs Java-Collections Java-Vector Practice Tags : JavaJava-Collections Similar Reads How to Replace an Element at a Specific Index of the Vector in Java? The Vector class implements a growable array of objects. Vectors basically fall in legacy classes but now it is fully compatible with collections. It is found in the java.util package and implements the List interface, so we can use all the methods of List interface here. Examples Input : Vector= [" 2 min read How to Fill (initialize at once) an Array in Java? An array is a group of like-typed variables that are referred to by a common name. In this, article we will learn about Filling array in Java while Initialization.Example:Java// Java program to fill the element in an array import java.util.*; public class Geeks { public static void main(String args[ 3 min read How to Create a HashSet With a Predefined Capacity in Java? In Java, we can create HashSet with a predefined capacity by creating a constructor and passing the capacity as a parameter to it. We have to initialize the capacity using a variable otherwise you can pass a direct value to it. Syntax:HashSet <datatype>myset=new HashSet<>(capacity);Here, 1 min read How to Create a HashSet with a Custom Initial Load Factor in Java? Java HashSet is a simple data structure provided by the Java Collection Framework that provides efficient storage and enables the storage of unique objects. One of the parameters that affect its performance is the load factor, which determines when the underlying hash table should be updated to acco 2 min read How to Implement a Custom Vector with Additional Functionality in Java? In Java, the Vector class is a part of the Java Collections Framework providing the Dynamic Arrays that can be resized. However, there might be scenarios where you need a Custom Vector with additional functionality tailored to your specific requirements. PrerequisiteTo follow along, you should have 2 min read Like