Java - Copy a List to Another List Example

This post shows you how to copy a List to another List with an example.

Copy a List to Another List Example

A simple way to copy a List is by using the constructor that takes a collection as its argument:
package net.javaguides.examples;

import java.util.ArrayList;
import java.util.List;

 * Different ways to copy a list into another list
 * @author Ramesh Fadatare
public class CopyListExamples {

    public static void main(String[] args) {

        List < String > fruits = new ArrayList < > ();
        // Adding new elements to the ArrayList

        // using constructor
        List < String > copyFruits = new ArrayList < > (fruits);
[Banana, Apple, mango, orange]
[Banana, Apple, mango, orange]
This approach isn't thread-safe. We may get ConcurrentAccessException error, to fix this problem we may need to use CopyOnWhiteArrayList, in which all mutative operations are implemented by making a fresh copy of the underlying array.
Checkout Copy a List to Another List in Java (5 Ways) - 5 different ways to copy a List to another List with an example.
  1. Using Constructor
  2. Using addAll() method
  3. Using Collections.copy() method
  4. Using Java 8
  5. Using Java 10


Java ArrayList Source Code Examples

Free Spring Boot Tutorial - 5 Hours Full Course

Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course