Java Program to Copy Elements of ArrayList to Vector
Last Updated :
10 Jun, 2022
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 collection to others, pass the object of ArrayList to the constructor of Vector while in initialization or copy element-wise using iteration.
Approach 1:
Vector<Integer> vector = new Vector<>(arrayList);
- Create a Vector object and while initialization passes the object of ArrayList in the constructor.
- Print the Vector.
Below is the implementation of the above approach:
Java
// Copy Elements of ArrayList to
// Java Vector using Constructor
import java.util.*;
class GFG {
public static void main(String[] args)
{
// create a list of string
ArrayList<String> L = new ArrayList<String>();
// add elements
L.add("Rohan");
L.add("Sangeeta");
L.add("Ritik");
L.add("Yogesh");
// create a vector and pass Object
// ArrayList into the constructor
Vector<String> V = new Vector<String>(L);
// print vector
System.out.println(V);
}
}
Output[Rohan, Sangeeta, Ritik, Yogesh]
Time Complexity: O(n), where n is the size of ArrayList.
Approach 2:
- Create a Vector.
- Start iterating the ArrayList.
- Add each element of ArrayList into the Vector.
- After completion of iteration, print the Vector.
Below is the implementation of the above approach:
Java
// Copy Elements of ArrayList to
// Java Vector by iteration and adding
// each element to the vector
import java.util.*;
class GFG {
public static void main(String[] args)
{
// create a list of string
ArrayList<String> L = new ArrayList<String>();
// add elements
L.add("Rohan");
L.add("Sangeeta");
L.add("Ritik");
L.add("Yogesh");
// create a vector
Vector<String> V = new Vector<String>();
for (String s : L)
V.add(s);
// print vector
System.out.println(V);
}
}
Output[Rohan, Sangeeta, Ritik, Yogesh]
Time Complexity: O(n), where n is the size of ArrayList.
Approach 3 : Using addAll() method
addAll() method is used to add all the elements from ArrayList to Vector. To use this method, we have to import the package java.util.Vector. To create ArrayList and Vector, we have to import the package util. So, we can simply import all this by specifying java.util.*.
Step 1: Declare the ArrayList and add the elements.
Step 2: Declare the Vector with the same type of ArrayList.
Step 3 : Now, specify the method Vector_Name.addAll(ArrayList_Name). It will simply add all the elements from ArrayList to the Vector.
Step 4: Print the Vector Elements.
Below is the implementation of the above approach:
Java
import java.util.*;
class GFG {
public static void main(String[] args)
{
ArrayList<String> L = new ArrayList<String>();
// add elements
L.add("geeksforgeeks");
L.add("learning");
L.add("platform");
System.out.println("ArrayList Elements : " + L);
// create a vector
Vector<String> V = new Vector<String>();
V.addAll(L); // ArrayList to Vector
// print vector
System.out.println("Vector Elements : " + V);
}
}
OutputArrayList Elements : [geeksforgeeks, learning, platform]
Vector Elements : [geeksforgeeks, learning, platform]
Approach 4 : Using List.copyOf()
This method is used in copying/adding the elements from ArrayList to Vector. To use this method, we have to import the package java.util.List.copyOf(). To create ArrayList and Vector, we have to import the package util. So, we can simply import all this by specifying java.util.*.
Step 1: Declare the ArrayList and add the elements.
Step 2: Declare the Vector with the same type of ArrayList.
Step 3 : Now, specify the method List.copyOf(ArrayList_Name) in the Vector constructor. It will simply add/copy all the elements from ArrayList to the Vector.
Step 4: Print the Vector Elements.
Below is the implementation of the above approach:
Java
import java.util.*;
class GFG {
public static void main(String[] args)
{
ArrayList<String> L = new ArrayList<String>();
// add elements
L.add("geeksforgeeks");
L.add("learning");
L.add("platform");
System.out.println("ArrayList Elements : " + L);
// create a vector
Vector<String> V
= new Vector<String>(List.copyOf(L));
// print vector
System.out.println("Vector Elements : " + V);
}
}
OutputArrayList Elements : [geeksforgeeks, learning, platform]
Vector Elements : [geeksforgeeks, learning, platform]
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
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
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 Search an Element in Vector
A vector in Java is a dynamic array that can be resized as needed. It is synchronized, which means it is safe in multi-threaded programs. To find an element in a Vector we have to loop through its elements to find a match. In this article, we will learn how to search for a component in a Vector usin
3 min read
Java Program to Add an Element to ArrayList using ListIterator
In this article, we will learn how to add an element to ArrayList using ListIterator. The ListIterator is used to return a list iterator over the list elements. The listIterator() returns an iterator over the list from the beginning, but the listIterator(index) returns an iterator over the list from
2 min read
Program to convert Array to Set in Java
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 of the array. In the case of primitives data types, the actual values are stored in contiguous memory locations. In cas
7 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
Java Program to Get the Maximum Element From a Vector
Prerequisite: Vectors in Java Why We Use Vector? Till now, we have learned two ways for declaring either with a fixed size of array or size enter as per the demand of the user according to which array is allocated in memory. int Array_name[Fixed_size] ; int array_name[variable_size] ; Both ways we l
4 min read
Reverse Order of All Elements of Java Vector
Vector class Implements a dynamic array means it can shrink and expand its size as required just likely having the same operations like that in the arrays. Don't confuse it with ArrayList as there is a thin line between vector and ArrayList, where the vector is synchronized rest the insertion order
4 min read
Java Program to Sort 2D Array Across Columns
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. This program is used to Sor
3 min read