Open In App

Vector addAll() Method in Java

Last Updated : 05 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In Java, the addAll() method is used to add all elements from a specified Collection to the current Vector. The addAll() method allows you to efficiently append multiple elements to a vector in one operation.

Implementation:

Java
// Java Program to addAll elements
// From Vector to Vector
import java.util.*;

class GFG 
{
    public static void main (String[] args) 
    {
      	// Created Vector
        Vector<Integer> v = new Vector<Integer>();
      	
      	// Created Second Vector 
      	Vector<Integer> a = new Vector<Integer>();
      	a.add(1);
      	a.add(2);
      	a.add(3);
      
      	// Add all elements of Vector a to Vector v
      	v.addAll(a);
      	
      	System.out.println("Vector : " + v);
    }
}

Output
Vector : [1, 2, 3]

Now there are two versions of Vector addAll() method i.e one with the specified index and one without any index.

1. java.util.Vector.addAll(Collection C):

This method is used to append all of the elements from the collection passed as a parameter to this function to the end of a vector keeping in mind the order of return by the collection's iterator. 

Syntax:

boolean addAll(Collection C)

Parameters: The method accepts a mandatory parameter C which is a collection of ArrayList. It is the collection whose elements are needed to be appended at the end of the vector. 

Return Value: The method returns True if at least one action of append is performed, else False

Below program illustrate the Java.util.Vector.addAll() method: 

Java
// Java code to illustrate boolean addAll()
import java.util.*;
import java.util.ArrayList;

public class GFG 
{
    public static void main(String args[])
    {
        // Creating an empty Vector
        Vector<String> v = new Vector<String>();

        // Use add() method to add elements in the Vector
        v.add("Geeks");
        v.add("for");
        v.add("Geeks");

        // A collection is created
        Collection<String> c = new ArrayList<String>();
        c.add("A");
        c.add("Computer");
        c.add("Portal");
        c.add("for");
        c.add("Geeks");

        // Displaying the Vector
        System.out.println("The vector is: " + v);

        // Appending the collection to the vector
        v.addAll(c);

        // Clearing the vector using clear() and displaying
        System.out.println("The new vector is: " + v);
    }
}

Output
The vector is: [Geeks, for, Geeks]
The new vector is: [Geeks, for, Geeks, A, Computer, Portal, for, Geeks]


2. java.util.Vector.addAll(int index, Collection C):

This method is used to append all of the elements from the collection passed as a parameter to this function at a specific index or position of a vector. 

Syntax:

boolean addAll(int index, Collection C)

Parameters: This function accepts two parameters as shown in the above syntax and are described below.

  • index: This parameter is of integer datatype and specifies the position in the vector starting from where the elements from the container will be inserted.
  • C: It is a collection of ArrayList. It is the collection whose elements are needed to be appended.

Example:

Java
// Java Program to addAll elements
// From Vector to Vector
import java.util.*;

class GFG 
{
    public static void main (String[] args) 
    {
      	// Created Vector
        Vector<Integer> v = new Vector<Integer>();
      	v.add(1);
      	v.add(4);
      
      	// Created Second Vector 
      	Vector<Integer> a = new Vector<Integer>();
      	a.add(2);
      	a.add(3);
      
      	int index=1;
      
      	// Add all elements of Vector a to Vector v
      	// at index 1
      	v.addAll(index,a);
      	
      	System.out.println("Vector : " + v);
    }
}

Output
Vector : [1, 2, 3, 4]




Next Article

Similar Reads