
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do you make a shallow copy of a list in Java?
In this article, we will learn how to make a shallow copy of a List in Java.
What is the List in Java?
List is a part of the Java collection framework. It stores elements sequentially and also allows us to store duplicate elements.
It is an interface, not a class, so it cannot be instantiated directly. However, there are several classes that implement the List interface, such as ArrayList, LinkedList, and Vector.
Shallow Copy of a List
A shallow copy means it is a copy of the original list, but elements in the copied list are still referencing the same objects as the original list. If you modify an object in the copied list, it will also affect the original list.
But if we try to modify primitive data types like integers, it will create a new object in memory. So, the original list remains unchanged.
How to make a shallow copy of a List in Java?
There are several ways to make a shallow copy of a List in Java:
Using ArrayList Constructor
We can create a shallow copy of a List by using the constructor of the ArrayList class. This constructor takes another List as an argument and creates a new ArrayList containing the elements of the specified List.
Example
In the example below, we will take an object called Person and create a property called name. We will then create a list of Person objects and make a shallow copy of it using the ArrayList constructor.
import java.util.ArrayList; import java.util.List; public class ShallowCopy{ public static void main(String[] args){ List<Person> originalList = new ArrayList<>(); originalList.add(new Person("Luffy")); originalList.add(new Person("Zoro")); originalList.add(new Person("Sanji")); System.out.println("The original list is: " + originalList); //shallow copy of the original list List<Person> shallowCopyList = new ArrayList<>(originalList); System.out.println("The shallow copy of the list is: " + shallowCopyList); //modifying the original list shallowCopyList.get(0).name = "Monkey D. Luffy"; System.out.println("The original list after modification is: " + originalList); System.out.println("The shallow copy of the list after modification is: " + shallowCopyList); } public static class Person { String name; public Person(String name) { this.name = name; } @Override public String toString() { return "Person{name='" + name + "'}"; } } }
Output
Following is the output of the above code:
The original list is: [Person{name='Luffy'}, Person{name='Zoro'}, Person{name='Sanji'}] The shallow copy of the list is: [Person{name='Luffy'}, Person{name='Zoro'}, Person{name='Sanji'}] The original list after modification is: [Person{name='Monkey D. Luffy'}, Person{name='Zoro'}, Person{name='Sanji'}] The shallow copy of the list after modification is: [Person{name='Monkey D. Luffy'}, Person{name='Zoro'}, Person{name='Sanji'}]
Using addAll() Method
We use the addAll() method to add all elements of one List to another List. This method creates a shallow copy of the original List.
Example
In this example, we will create a List of integers and then create a shallow copy of it using the addAll() method.
import java.util.ArrayList; import java.util.List; public class ShallowCopy{ public static void main(String[] args){ List<Integer> originalList = new ArrayList<>(); originalList.add(1); originalList.add(2); originalList.add(3); System.out.println("The original list is: " + originalList); //shallow copy of the original list List<Integer> shallowCopyList = new ArrayList<>(); shallowCopyList.addAll(originalList); System.out.println("The shallow copy of the list is: " + shallowCopyList); //modifying the original list shallowCopyList.set(0, 10); System.out.println("The original list after modification is: " + originalList); System.out.println("The shallow copy of the list after modification is: " + shallowCopyList); } }
Output
Following is the output of the above code:
The original list is: [1, 2, 3] The shallow copy of the list is: [1, 2, 3] The original list after modification is: [1, 2, 3] The shallow copy of the list after modification is: [10, 2, 3]
Note: Here, the original list is not changed because when we try to modify primitive data types like integers, it creates a new object in memory. So, the original list remains unchanged.
Using copyOf() Method
We can also create a shallow copy of a List using the copyOf() method. This method is available in the List
interface and creates an unmodifiable shallow copy of the original List.
Example
In the below example, we will create an object called Book and will create a property called title. We will then create a list of Book objects and make a shallow copy of it using the copyOf() method.
import java.util.ArrayList; import java.util.List; public class ShallowCopy{ public static void main(String[] args){ List<Book> originalList = new ArrayList<>(); originalList.add(new Book("Java Programming")); originalList.add(new Book("Python Programming")); originalList.add(new Book("C++ Programming")); System.out.println("The original list is: " + originalList); //shallow copy of the original list List<Book> shallowCopyList = List.copyOf(originalList); System.out.println("The shallow copy of the list is: " + shallowCopyList); //modifying the original list shallowCopyList.get(0).title = "Advanced Java Programming"; System.out.println("The original list after modification is: " + originalList); System.out.println("The shallow copy of the list after modification is: " + shallowCopyList); } public static class Book { String title; public Book(String title) { this.title = title; } @Override public String toString() { return "Book{title='" + title + "'}"; } } }
Output
Following is the output of the above code:
The original list is: [Book{title='Java Programming'}, Book{title='Python Programming'}, Book{title='C++ Programming'}] The shallow copy of the list is: [Book{title='Java Programming'}, Book{title='Python Programming'}, Book{title='C++ Programming'}] The original list after modification is: [Book{title='Advanced Java Programming'}, Book{title='Python Programming'}, Book{title='C++ Programming'}] The shallow copy of the list after modification is: [Book{title='Advanced Java Programming'}, Book{title='Python Programming'}, Book{title='C++ Programming'}]
Using stream() Method
We can also create a shallow copy of a List using the stream() method. This method is available in the List
interface and creates a stream of the original List, which can then be collected into a new List.
Example
In the below example, we will create an object called Employee and create a property called name. We will then create a list of Employee objects and make a shallow copy of it using the stream() method.
import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class ShallowCopy{ public static void main(String[] args){ List<Employee> originalList = new ArrayList<>(); originalList.add(new Employee("Sasuke")); originalList.add(new Employee("Naruto")); originalList.add(new Employee("Sakura")); System.out.println("The original list is: " + originalList); //shallow copy of the original list List<Employee> shallowCopyList = originalList.stream().collect(Collectors.toList()); System.out.println("The shallow copy of the list is: " + shallowCopyList); //modifying the original list shallowCopyList.get(0).name = "Sai"; System.out.println("The original list after modification is: " + originalList); System.out.println("The shallow copy of the list after modification is: " + shallowCopyList); } public static class Employee { String name; public Employee(String name) { this.name = name; } @Override public String toString() { return "Employee{name='" + name + "'}"; } } }
Output
Following is the output of the above code:
The original list is: [Employee{name='Sasuke'}, Employee{name='Naruto'}, Employee{name='Sakura'}] The shallow copy of the list is: [Employee{name='Sasuke'}, Employee{name='Naruto'}, Employee{name='Sakura'}] The original list after modification is: [Employee{name='Sai'}, Employee{name='Naruto'}, Employee{name='Sakura'}] The shallow copy of the list after modification is: [Employee{name='Sai'}, Employee{name='Naruto'}, Employee{name='Sakura'}]