In this tutorial, I will show you 5 different ways to copy a List to another List with an example.
- Using Constructor
- Using addAll() method
- Using Collections.copy() method
- Using Java 8
- 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.
Learn and master Java Collections Framework at Learn Java Collections Framework
Collections Examples
- Java LinkedHashMap Example
- Java HashSet Example
- Java LinkedList Example
- Java ArrayList Example
- How To Remove Duplicate Elements From ArrayList In Java?
- Different Ways to Iterate over List, Set, and Map in Java
- Java Comparator Interface Example
- Java Comparable Interface Example
- Java IdentityHashMap Example
- Java WeakHashMap Example
- Java EnumMap Example
- Java CopyOnWriteArraySet Example
- Java EnumSet Class Example
- Guide to Java 8 forEach Method
- Different Ways to Iterate over a List in Java [Snippet]
- Different Ways to Iterate over a Set in Java [Snippet]
- Different Ways to Iterate over a Map in Java [Snippet]
- Iterate over TreeSet in Java Example
- Iterate over LinkedHashSet in Java Example
- Remove First and Last Elements of LinkedList in Java
- Iterate over LinkedList using an Iterator in Java
- Search an Element in an ArrayList in Java
- Iterate over ArrayList using Iterator in Java
- Remove Element from HashSet in Java
- Iterating over a HashSet using Iterator
References
Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours
Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course