Copy a List to Another List in Java (5 Ways)

In this tutorial, I will show you 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

1. Using Constructor

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
        fruits.add("Banana");
        fruits.add("Apple");
        fruits.add("mango");
        fruits.add("orange");
        System.out.println(fruits);

        // using constructor
        List < String > copyFruits = new ArrayList < > (fruits);
        System.out.println(copyFruits);
    }
}
Output:
[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 CopyOnWriteArrayList, in which all mutative operations are implemented by making a fresh copy of the underlying array.

2. Using addAll() method

Another approach to copy elements is using the addAll() method:
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
        fruits.add("Banana");
        fruits.add("Apple");
        fruits.add("mango");
        fruits.add("orange");
        System.out.println(fruits);

        // using the addAll method
        List < String > copy = new ArrayList < > ();
        copy.addAll(fruits);
        System.out.println(copy);
    }
}
Output:
[Banana, Apple, mango, orange]
[Banana, Apple, mango, orange]

3. Using Collections.copy() method

The Collections class consists exclusively of static methods that operate on or return collections.
One of them is a copy() method, which needs a source list and a destination list at least as long as the source.
This method copies all elements of the source list to the destination list. After the copy index of the elements in both source and destination lists would be identical.
The destination list must be long enough to hold all copied elements. If it is longer than that, the rest of the destination list's elements would remain unaffected.
package net.javaguides.examples;

import java.util.ArrayList;
import java.util.Collections;
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 > fruits1 = new ArrayList < > ();
        // Adding new elements to the ArrayList
        fruits1.add("Banana");
        fruits1.add("Apple");
        fruits1.add("mango");
        fruits1.add("orange");
        System.out.println(fruits1);

        // using the Collections.copy() method
        List < String > fruits2 = new ArrayList < > (fruits1.size());
        fruits2.add("1");
        fruits2.add("2");
        fruits2.add("3");
        fruits2.add("4");
        Collections.copy(fruits2, fruits1);

        System.out.println(fruits2);
    }
}
Output:
[Banana, Apple, mango, orange]
[Banana, Apple, mango, orange]

4. Using Java 8

Let's use Java 8 Stream APIs to copy List into another List:
package net.javaguides.examples;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 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
        fruits.add("Banana");
        fruits.add("Apple");
        fruits.add("mango");
        fruits.add("orange");
        System.out.println(fruits);

        // using Java 8 Stream APIs
        List < String > copy = fruits.stream()
            .collect(Collectors.toList());

        System.out.println(copy);
    }
}
Output:
[Banana, Apple, mango, orange]
[Banana, Apple, mango, orange]

5. Using Java 10

Finally, one of the last Java version allows us to create an immutable list containing the elements of the given Collection:
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
        fruits.add("Banana");
        fruits.add("Apple");
        fruits.add("mango");
        fruits.add("orange");
        System.out.println(fruits);

        // using Java 8 copyOf() method
        List < String > copy = List.copyOf(fruits);
        System.out.println(copy);
    }
}
Output:
[Banana, Apple, mango, orange]
[Banana, Apple, mango, orange]

Conclusion

In this tutorial, we've explored 5 different ways to copy a List to another List with different Java versions.

References

Comments

  1. Dear Sir... Thankyou for a good piece of information..
    But just to Correct..
    Won't it be CopyonWriteArrayList

    ReplyDelete
    Replies
    1. Yes we can use CopyonWriteArrayList and this i have already covered in first approach (thread-safe). Cheers!.

      Delete

Post a Comment