- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Append all elements of another Collection to a Vector in Java
The elements of a Collection can be appended at the end of the Vector using the method java.util.Vector.addAll(). This method takes a single parameter i.e. the Collection whose elements are added to the Vector and it returns true if the Vector is changed.
A program that demonstrates this is given as follows −
Example
import java.util.Vector; public class Demo { public static void main(String args[]) { Vector vec1 = new Vector(); vec1.add(7); vec1.add(3); vec1.add(5); vec1.add(9); vec1.add(2); System.out.println("The Vector vec1 elements are: " + vec1); Vector vec2 = new Vector(); vec2.add(1); vec2.add(8); vec2.add(6); vec2.addAll(vec1); System.out.println("The Vector vec2 elements are: " + vec2); } }
The output of the above program is as follows −
The Vector vec1 elements are: [7, 3, 5, 9, 2] The Vector vec2 elements are: [1, 8, 6, 7, 3, 5, 9, 2]
Now let us understand the above program.
First, the Vector vec1 is defined and elements are added using the Vector.add() method. Then Vector vec1 is displayed. A code snippet which demonstrates this is as follows −
Vector vec1 = new Vector(); vec1.add(7); vec1.add(3); vec1.add(5); vec1.add(9); vec1.add(2); System.out.println("The Vector vec1 elements are: " + vec1);
Then the Vector vec2 is defined and elements are added using the Vector.add() method. After that the Vector.addAll() method is used to append all the vec1 elements at the end of vec2. Then Vector vec2 is displayed. A code snippet which demonstrates this is as follows −
Vector vec2 = new Vector(); vec2.add(1); vec2.add(8); vec2.add(6); vec2.addAll(vec1); System.out.println("The Vector vec2 elements are: " + vec2);
- Related Articles
- Append all elements of other Collection to ArrayList in Java
- Java Program to append all elements of other Collection to ArrayList
- How to remove all elements from a Collection in another Collection
- How to retain elements from a Collection in another Collection
- Remove all the elements from an ArrayList that are in another Collection in Java
- Clear out all of the Vector elements in Java
- Replace all the elements of a Vector with Collections.fill() in Java
- How to replace one vector elements with another vector elements in R?
- R programming to subtract all values in a vector from all values in another vector.
- How to append a vector in a vector in C++?
- Add all the elements from a collection to the HashSet in Java
- Insert all elements of other Collection to Specified Index of Java ArrayList
- Add elements to a Vector in Java
- Rotate elements of a collection in Java
- Java Program to insert all elements of other Collection to specified Index of ArrayList
