

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do I insert all elements from one list into another in Java?
Solution
We can add all elements of one list into another list easily using its addAll() method.
Syntax
boolean addAll(Collection<? extends E> c)
Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator.
Type Parameter
E − The runtime type of the collection passed.
Parameters
c − Collection containing elements to be added to this list.
Returns
True if this list changed as a result of the call
Throws
UnsupportedOperationException − If the addAll operation is not supported by this list.
ClassCastException − If the class of an element of the specified collection prevents it from being added to this list.
NullPointerException − If the specified collection contains one or more null elements and this list does not permit null elements, or if the specified collection is null.
IllegalArgumentException − If some property of an element of the specified collection prevents it from being added to this list.
Example
The following example shows how to add all elements from a list into another list using the addAll() method.
package com.tutorialspoint; import java.util.ArrayList; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { // Create a list object List<Integer> list = new ArrayList<>(); // add elements to the list list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); list.add(6); // print the list System.out.println(list); List<Integer> list1 = new ArrayList<>(); list1.add(0); list1.addAll(list); System.out.println(list1); } }
Output
This will produce the following result −
[1, 2, 3, 4, 5, 6] [0, 1, 2, 3, 4, 5, 6]
- Related Questions & Answers
- How do I insert a record from one Mongo database into another?
- How do I INSERT INTO from one MySQL table into another table and set the value of one column?
- How do I insert elements in a Java list?
- How to insert values from one table into another in PostgreSQL?
- How do I insert elements at a specific index in Java list?
- Copy all the elements from one set to another in Java
- How do I remove multiple elements from a list in Java?
- How do you copy an element from one list to another in Java?
- Python program to copy all elements of one array into another array
- Take all records from one MySQL table and insert it to another?
- MySQL statement to copy data from one table and insert into another table
- Java Program to copy all the key-value pairs from one Map into another
- How do I turn a list into an array in Java?
- Insert a String into another String in Java
- Insert data from one table to another in MySQL?