
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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?
In this article, we will learn how to insert all elements from one list into another in Java. This is a general operation we perform when working with lists while solving problems in Java.
We will cover the following methods to insert all elements from one list into another:
Using addAll() Method
We can add all elements of one list into another list easily using its addAll() method.
This method 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.
Example
The following example shows how to add all elements from a list into another list using the addAll() method.
import java.util.ArrayList; import java.util.List; public class InsertAllElements { public static void main(String[] args) { List<Integer> sourceList = new ArrayList<>(); sourceList.add(1); sourceList.add(2); sourceList.add(3); List<Integer> destinationList = new ArrayList<>(); destinationList.add(4); destinationList.add(5); System.out.println("Destination List before adding elements: " + destinationList); destinationList.addAll(sourceList); System.out.println("Destination List after adding elements: " + destinationList); } }
Output
Following is the output of the above code:
Destination List before adding elements: [4, 5] Destination List after adding elements: [4, 5, 1, 2, 3]
Using For Loop
We will use a for loop to iterate through the source list and add each element to the destination list using the add() method.
Example
In the following example, we will create a source list and a destination list, and then use a for loop to insert all elements from the source list into the destination list.
import java.util.ArrayList; import java.util.List; public class InsertAllElements { public static void main(String[] args) { List<Integer> sourceList = new ArrayList<>(); sourceList.add(1); sourceList.add(2); sourceList.add(3); List<Integer> destinationList = new ArrayList<>(); destinationList.add(4); destinationList.add(5); System.out.println("Destination List before adding elements: " + destinationList); for (Integer element : sourceList) { destinationList.add(element); } System.out.println("Destination List after adding elements: " + destinationList); } }
Output
Following is the output of the above code:
Destination List before adding elements: [4, 5] Destination List after adding elements: [4, 5, 1, 2, 3]
Using Stream API
Now, let's use the Stream API to insert all elements from one list into another. We can use the flatMap() method to flatten the source list and then collect the results into the destination list.
Example
In the following example, we will create a source list and a destination list, and then use the Stream API to insert all elements from the source list into the destination list.
import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class InsertAllElements { public static void main(String[] args) { List<Integer> sourceList = new ArrayList<>(); sourceList.add(1); sourceList.add(2); sourceList.add(3); List<Integer> destinationList = new ArrayList<>(); destinationList.add(4); destinationList.add(5); System.out.println("Destination List before adding elements: " + destinationList); destinationList.addAll(sourceList.stream().collect(Collectors.toList())); System.out.println("Destination List after adding elements: " + destinationList); } }
Output
Following is the output of the above code:
Destination List before adding elements: [4, 5] Destination List after adding elements: [4, 5, 1, 2, 3]
Using ListIterator
We can use a ListIterator to traverse the source list and add each element to the destination list.
Example
In the following example, we will create a source list and a destination list, and then use a ListIterator to insert all elements from the source list into the destination list.
import java.util.ArrayList; import java.util.List; import java.util.ListIterator; public class InsertAllElements { public static void main(String[] args) { List<Integer> sourceList = new ArrayList<>(); sourceList.add(1); sourceList.add(2); sourceList.add(3); List<Integer> destinationList = new ArrayList<>(); destinationList.add(4); destinationList.add(5); System.out.println("Destination List before adding elements: " + destinationList); ListIterator<Integer> iterator = sourceList.listIterator(); while (iterator.hasNext()) { destinationList.add(iterator.next()); } System.out.println("Destination List after adding elements: " + destinationList); } }
Following is the output of the above code:
Destination List before adding elements: [4, 5] Destination List after adding elements: [4, 5, 1, 2, 3]