How to Implement a Custom Vector with Additional Functionality in Java? Last Updated : 23 Feb, 2024 Comments Improve Suggest changes Like Article Like Report 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 a Basic understanding of Java programming particularly knowledge of classes, arrays, and Java Collections Framework. Example Java // Java Program to implement a custom // Vector with additional functionality import java.io.*; import java.util.Vector; // CustomVector class extending Vector class GFG<E> extends Vector<E> { // Method to calculate sum of elements public int calculateSum() { int sum = 0; // Iterating over elements for (E element : this) { // Checking if element is an Integer if (element instanceof Integer) { // Adding element to sum sum += (Integer) element; } } // Return sum of elements return sum; } } // Driver Class public class Main { // Main Function public static void main(String[] args) { // Creating an instance of the CustomVector GFG<Integer> customVector = new GFG<>(); // Adding elements to custom vector customVector.add(10); customVector.add(20); customVector.add(30); // Calculating the sum using custom method int sum = customVector.calculateSum(); // Displaying the result System.out.println("Sum of elements: " + sum); } } Output: Sum of elements: 60Explaination of the above Program:Method calculateSum() calculates the sum of elements in the vector.The calculateSum() method iterates through the elements of the vector and checks if each element is an instance of Integer. If it is, it adds it to the sum.In the main method, an instance of GFG with a type of Integer is created.Three integers are added to this GFG instance using the add() method.The calculateSum() method is then called to calculate the sum of the integers in the vector.Finally, the sum is printed to the console. Comment More infoAdvertise with us Next Article How to Implement a Custom Vector with Additional Functionality in Java? M mguru4c05q Follow Improve Article Tags : Java Java Programs Java-Vector Java Examples Practice Tags : Java Similar Reads How to Initialize a Vector with a Specific Initial Capacity in Java? 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 lear 2 min read Copy Elements of Vector to Java ArrayList Since Vector class and ArrayList class both are part of Java Collections, ie Collection framework, so both of these classes can use methods available to the Collection framework. Copy() method is one of the methods of Collection Interface which is used to copy one list to another list, here list can 3 min read Convert ArrayList to Vector in Java There are several ways to convert ArrayList to Vector. We can use a vector constructor for converting ArrayList to vector. We can read ArrayList elements one by one and add them in vector. Approach 1: (Using Vector Constructor) Create an ArrayList.Add elements in ArrayList.Create a vector and pass t 3 min read Copy Elements of One Java Vector to Another Vector in Java Vector is similar to arrays but is growable also, or we can say no fixed size is required. Previously vector was a part of legacy classes but now it is part of Collections. It also implements a List interface, so we can use any method of list interface on vectors also. Syntax : Vector<Integer> 3 min read Java Program to Copy Elements of ArrayList to Vector Vector implements List Interface, like ArrayList it also maintains insertion order but it is rarely used in the non-thread environment as it is synchronized, and due to which it gives a poor performance in adding, searching, deleting, and updating of its elements. To copy elements from one collectio 4 min read Like