sneppets-java8

How to copy contents of a List to another List in Java

This tutorial shows you how to copy contents of a List to another List in Java.

Copy a List to Another List

The following example shows different ways to copy contents of a List to another list.

package com.sneppets.example;

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

public class CopyListAToListBExample {
	
	public static void main (String[] args) {
		
		List<String> listA = new ArrayList<>(10);
		listA.add("a");
		listA.add("b");
		listA.add("c");
		System.out.println("List A: " + listA);
		
		//Method 1: Constructor
		List<String> listB = new ArrayList<>(listA);
		System.out.println("List B copied using constructor: " + listB);
		
		//Method 2: Collections.copy() method
		//Note the destination list should be as long as source list to hold the entire source list
		List<String> listC = new ArrayList<>();
		listC.add("x");
		listC.add("y");
		listC.add("z");
		System.out.println("List C Before Copy: " + listC);
		Collections.copy(listC, listA);
		System.out.println("List C After Copy using Collections.copy: " + listC);
		
		//Method 3: addAll() method
		List<String> listD = new ArrayList<>();
		listD.addAll(listA);
		System.out.println("List D copied using addAll() : " + listD);
		
		//Method 4: Java 8 stream
		List<String> listE = listA.stream().collect(Collectors.toList());
		System.out.println("List E copied using Java 8 stream : " + listE);
		
		//Method 5: Java 10 copyOf()
		List<String> listF = List.copyOf(listA);
		System.out.println("List F copied using Java 10 : " + listF);
		
	}

}

Output

List A: [a, b, c]
List B copied using constructor: [a, b, c]
List C Before Copy: [x, y, z]
List C After Copy using Collections.copy: [a, b, c]
List D copied using addAll() : [a, b, c]
List E copied using Java 8 stream : [a, b, c]
List F copied using Java 10 : [a, b, c]

1. Constructor

A simple way to copy a List is using the constructor that constructs a list containing the elements of specified collection,  in the order they are returned by the Collection’s iterator

List<String> listB = new ArrayList<>(listA);

2. Collections.copy()

You can use Collections class’s static method copy(dest, src) which copies all of the elements from source list to destination list.

Collections.copy(listC, listA);

Note, the destination list must be at least as long as source list and if the destination list is too small to hold the entire source list, then this method will throw IndexOutOfBoundsException like

Exception in thread "main" java.lang.IndexOutOfBoundsException: Source does not fit in dest

3. List.addAll()

Anothery way to copy List is using addAll() method

listD.addAll(listA);

4. Java 8 stream()

Using Java 8 stream perform collect operation on the stream and then use Collectors.toList() to accumulate elements into a List as shown.

List<String> listE = listA.stream().collect(Collectors.toList());

5. Java 10 List.copyOf()

Finally using Java 10 it allows you to create an unmodifiable List containing the elements of the specified collection.

List<String> listF = List.copyOf(listA);

Also See

References

Leave a Reply

avatar
  Subscribe  
Notify of