To create a new ArrayList from an existing ArrayList in Java, you can use the ArrayList constructor that accepts a Collection as a parameter.
Here's an example:
import java.util.ArrayList;
import java.util.List;
public class ArrayListExample {
public static void main(String[] args) {
ArrayList<String> originalList = new ArrayList<>();
originalList.add("Element 1");
originalList.add("Element 2");
originalList.add("Element 3");
ArrayList<String> newList = new ArrayList<>(originalList);
// Print the elements of the new list
for (String element : newList) {
System.out.println(element);
}
}
}
In the code above, we create an originalList ArrayList and add some elements to it. Then, we create a new ArrayList newList by passing originalList as a parameter to the ArrayList constructor. This constructor initializes the new ArrayList with the elements of the originalList.
Finally, we iterate over the elements of the newList and print them.
Output:
Element 1
Element 2
Element 3
By using this approach, you can create a new ArrayList with the same elements as the original one. Note that the elements themselves are not copied, only references to them are stored in the new ArrayList. If you modify the elements in the originalList or newList, the changes will be reflected in both lists since they reference the same objects.
Example 2: Create ArrayList from Another ArrayList in Java
This example 2 demonstrates the following operations:
- Create an ArrayList from another collection using the ArrayList(Collection c) constructor.
- Adding all the elements from an existing collection to the new ArrayList using the addAll() method.
import java.util.ArrayList;
import java.util.List;
public class CreateArrayListFromCollectionExample {
public static void main(String[] args) {
List<Integer> firstFivePrimeNumbers = new ArrayList<>();
firstFivePrimeNumbers.add(2);
firstFivePrimeNumbers.add(3);
firstFivePrimeNumbers.add(5);
firstFivePrimeNumbers.add(7);
firstFivePrimeNumbers.add(11);
// Creating an ArrayList from another collection
List<Integer> firstTenPrimeNumbers = new ArrayList<>(firstFivePrimeNumbers);
List<Integer> nextFivePrimeNumbers = new ArrayList<>();
nextFivePrimeNumbers.add(13);
nextFivePrimeNumbers.add(17);
nextFivePrimeNumbers.add(19);
nextFivePrimeNumbers.add(23);
nextFivePrimeNumbers.add(29);
// Adding an entire collection to an ArrayList
firstTenPrimeNumbers.addAll(nextFivePrimeNumbers);
System.out.println(firstTenPrimeNumbers);
}
}
Output
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
Reference
Java ArrayList Source Code Examples
- Sort List of Integers in Ascending and Descending Order Example
- List (ArrayList) Iterator Example
- Create the Immutable ArrayList with List.of() Method Example
- Create Immutable ArrayList with Collections.unmodifiableList() Exmaple
- Java 10 - Copy List into Another List Exmaple
- Java 8 - Copy List into Another List Example
- Java - Copy a List to Another List using Collections.copy() method
- Java - Copy a List to Another List Example
- Java ArrayList spliterator() Method Example
- Java ArrayList sort() Method Example
- Java ArrayList retainAll() Method Example
- Java ArrayList removeIf() Method Example
- Java ArrayList removeAll() Method Example
- Java ArrayList remove() Method Example
- Java ArrayList lastIndexOf() Method Example
- Java ArrayList isEmpty() Method Example
- Java util ArrayList indexOf() Method Example
- Java ArrayList get() Method Example
- Java ArrayList ensureCapacity() Method Example
- Java ArrayList contains() Method Example
- Java ArrayList clone() Method Example
- Java ArrayList clear() Method Example
- Java ArrayList addAll() Method Example
- Java ArrayList add() Method Example
- Java 8 forEach() List Example
- Add Enum Values to ArrayList Example
- Join List Strings with Commas in Java
- Java Stream filter null values example
- Java ArrayList subList() Example
- Get Index of Elements in ArrayList Example
- Java ArrayList removeIf() Example
- Java ArrayList add(), get() and set() Method Example
- Iterate over ArrayList Using forEach Java
- Iterate over ArrayList using Iterator in Java
- Java ArrayList indexOf() and lastIndexOf() Example
- Search an Element in an ArrayList in Java
- Clear ArrayList in Java Example
- Java ArrayList removeAll() Method Example
- Java ArrayList remove() Method Example
- How to Iterate over ArrayList in Java
- How to Remove Element from ArrayList in Java
- How to Access Elements of ArrayList in Java
- Create ArrayList from Another ArrayList in Java
- How to Create an ArrayList and Add Elements to It