Convert ArrayList to Vector in Java
Last Updated :
10 Feb, 2022
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 the ArrayList in Vector Constructor.
Vector(Collection c): Creates a vector that contains the elements of collection c.
Vector<E> v = new Vector<E>(Collection c);
Example:
Java
// Java program to Convert ArrayList to Vector
import java.util.ArrayList;
import java.util.Vector;
public class GFG {
public static void main(String[] args)
{
// create ArrayList
ArrayList<Integer> ArrList = new ArrayList<Integer>();
// add elements in ArrayList
ArrList.add(10);
ArrList.add(20);
ArrList.add(30);
ArrList.add(40);
ArrList.add(50);
// display ArrayList
System.out.println(" ArrayList : " + ArrList);
// create vector and pass the ArrayList in vector
// constructor
Vector<Integer> vector = new Vector<Integer>(ArrList);
// print vector
System.out.println(" Vector : " + vector);
}
}
Output ArrayList : [10, 20, 30, 40, 50]
Vector : [10, 20, 30, 40, 50]
Approach 2: (Using for loop)
- Create a ArrayList.
- Add some values in ArrayList.
- Create an Vector.
- Execute a loop.
- Traverse each element of ArrayList from the left side to the right side.
- Add the ArrayList elements in Vector.
Example:
Java
// Java program to Convert ArrayList to Vector
import java.util.Vector;
import java.util.ArrayList;
public class GFG {
public static void main(String[] args)
{
// Create a ArrayList that contain strings
ArrayList<String> Arrlist = new ArrayList<String>();
// add values in ArrayList
Arrlist.add("A");
Arrlist.add("B");
Arrlist.add("C");
Arrlist.add("D");
Arrlist.add("E");
// Display the ArrayList
System.out.println(" ArrayList : " + Arrlist);
// create a vector
Vector<String> v = new Vector<String>();
// Convert ArrayList to Vector
// get the size to ArrayList
int n = Arrlist.size();
// execute for loop from 0 to n
for (int i = 0; i < n; i++) {
// get the elements from ArrayList
// and add the arrayList elements in vector
v.add(Arrlist.get(i));
}
// Display Vector
System.out.println("\n vector : " + v);
}
}
Output ArrayList : [A, B, C, D, E]
vector : [A, B, C, D, E]
Approach 3: (Using addAll() method)
This method is used to append all 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.
Example:
Java
// Java program to Convert ArrayList to Vector
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
ArrayList<String> listStrings = new ArrayList<>();
listStrings.add("Geeks");
listStrings.add("for");
listStrings.add("Geeks");
// create empty vector object
Vector<String> vStrings = new Vector<>();
// use the addAll method
vStrings.addAll(listStrings);
System.out.println("Vector contains: " + vStrings);
}
}
OutputVector contains: [Geeks, for, Geeks]
Similar Reads
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
Program to Convert a Vector to List in Java Given a Vector, the task is to Convert Vector to List in Java Examples: Input: Vector: [1, 2, 3, 4, 5] Output: List: [1, 2, 3, 4, 5] Input : Vector = [a, b, c, d, e, f] Output : List = [a, b, c, d, e, f] Using Collections.list() method Syntax: List list = Collections.list(vec.elements()); Approach:
3 min read
Java Program to Convert an Array into a List In Java, arrays and lists are two commonly used data structures. While arrays have a fixed size and are simple to use, lists are dynamic and provide more flexibility. There are times when you may need to convert an array into a list, for instance, when you want to perform operations like adding or r
4 min read
Program to Convert Stream to an Array in Java A Stream is a sequence of objects that support various methods which can be pipelined to produce the desired result. An array is a group of like-typed variables that are referred to by a common name. An array can contain primitives data types as well as objects of a class depending on the definition
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