
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to copy a list to another list in Java?
A List of elements can be copied to another List using multiple ways.
Way #1
Create a List by passing another list as a constructor argument.
List<String> copyOflist = new ArrayList<>(list);
Create a List and use addAll method to add all the elements of the source list.
Way #2
List<String> copyOfList = new ArrayList<>(); copyOfList.addAll(list);
Way #3
Use Collections.copy method to copy the contents of source list to destination. Existing elements will be overridden by indexes if any.
Collections.copy(copyOfList, list);
Way #4
Use Streams to create a copy of a list.
List<String> copyOfList = list.stream().collect(Collectors.toList());
Example
Following is the example to explain the creation of copy of List object using multiple ways −
package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; public class CollectionsDemo { public static void main(String[] args) { List<String> list = Arrays.asList("Zara","Mahnaz","Ayan" ); System.out.println("Source: " + list); List<String> copyOfList1 = new ArrayList<>(list); System.out.println("Copy 1: " + copyOfList1); List<String> copyOfList2 = new ArrayList<>(); copyOfList2.addAll(list); System.out.println("Copy 2: " + copyOfList2); List<String> copyOfList3 = Arrays.asList("Rob","Julie","John" ); Collections.copy(copyOfList3, list); System.out.println("Copy 3: " + copyOfList3); List<String> copyOfList4 = list.stream().collect(Collectors.toList()); System.out.println("Copy 4: " + copyOfList4); } }
Output
This will produce the following result −
Source: [Zara, Mahnaz, Ayan] Copy 1: [Zara, Mahnaz, Ayan] Copy 2: [Zara, Mahnaz, Ayan] Copy 3: [Zara, Mahnaz, Ayan] Copy 4: [Zara, Mahnaz, Ayan]
- Related Articles
- Java Program to copy value from one list to another list
- How do you copy an element from one list to another in Java?
- How do you copy a list in Java?
- Python How to copy a nested list
- How to clone or copy a list in Python?
- How to clone or copy a list in Kotlin?
- How to copy a List collection to an array?
- How to copy or clone a C# list?
- How to remove index list from another list in python?
- How do I copy items from list to list without foreach in C#?
- How do you make a shallow copy of a list in Java?
- C# program to clone or copy a list
- Python program to clone or copy a list.
- Merge a linked list into another linked list at alternate positions in Java
- How to shuffle a List in Java

Advertisements