The copy() method in Java provides a way to copy elements from one list to another. This method is part of the java.util.Collections class and is particularly useful for transferring elements between lists while maintaining the order of elements.
Table of Contents
- Introduction
copy()Method Syntax- Examples
- Basic Usage of
copy() - Using
copy()with Custom Classes
- Basic Usage of
- Real-World Use Case
- Conclusion
Introduction
The Collections.copy() method allows you to copy all elements from a source list into a destination list. The destination list must have at least as many elements as the source list, as the copy() method replaces elements in the destination list rather than adding to it. This method is useful for tasks such as cloning lists or synchronizing list contents between different parts of an application.
copy() Method Syntax
The syntax for the copy() method is as follows:
public static <T> void copy(List<? super T> dest, List<? extends T> src)
Parameters:
dest: The destination list into which elements are copied. It must be at least as long as the source list.src: The source list from which elements are copied.
Throws:
IndexOutOfBoundsExceptionif the destination list is shorter than the source list.NullPointerExceptionif either of the specified lists is null.
Examples
Basic Usage of copy()
The following example demonstrates how to use the copy() method to copy elements from one list to another.
Example
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CopyExample {
public static void main(String[] args) {
// Create source and destination lists
List<String> sourceList = new ArrayList<>();
Collections.addAll(sourceList, "Apple", "Banana", "Cherry");
List<String> destinationList = new ArrayList<>(Collections.nCopies(sourceList.size(), ""));
// Copy elements from source to destination
Collections.copy(destinationList, sourceList);
System.out.println("Source List: " + sourceList);
System.out.println("Destination List: " + destinationList);
}
}
Output:
Source List: [Apple, Banana, Cherry]
Destination List: [Apple, Banana, Cherry]
Using copy() with Custom Classes
You can also use the copy() method with lists containing instances of custom classes.
Example
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class Student {
String name;
Student(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}
public class CustomCopyExample {
public static void main(String[] args) {
// Create source and destination lists
List<Student> sourceList = new ArrayList<>();
sourceList.add(new Student("Amit"));
sourceList.add(new Student("Neha"));
sourceList.add(new Student("Raj"));
List<Student> destinationList = new ArrayList<>(Collections.nCopies(sourceList.size(), null));
// Copy elements from source to destination
Collections.copy(destinationList, sourceList);
System.out.println("Source List: " + sourceList);
System.out.println("Destination List: " + destinationList);
}
}
Output:
Source List: [Amit, Neha, Raj]
Destination List: [Amit, Neha, Raj]
Real-World Use Case
Synchronizing List Contents
In real-world applications, the copy() method can be used to synchronize the contents of two lists, ensuring that the destination list always reflects the current state of the source list. This is useful in scenarios where multiple parts of an application need to work with consistent data.
Example
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class Product {
String name;
double price;
Product(String name, double price) {
this.name = name;
this.price = price;
}
@Override
public String toString() {
return name + " ($" + price + ")";
}
}
public class SynchronizeListExample {
public static void main(String[] args) {
// Source list of products
List<Product> sourceProducts = new ArrayList<>();
sourceProducts.add(new Product("Laptop", 1500.00));
sourceProducts.add(new Product("Smartphone", 800.00));
sourceProducts.add(new Product("Tablet", 600.00));
// Destination list, initially empty
List<Product> destinationProducts = new ArrayList<>(Collections.nCopies(sourceProducts.size(), null));
// Synchronize destination list with source list
Collections.copy(destinationProducts, sourceProducts);
System.out.println("Source Products: " + sourceProducts);
System.out.println("Destination Products: " + destinationProducts);
}
}
Output:
Source Products: [Laptop ($1500.0), Smartphone ($800.0), Tablet ($600.0)]
Destination Products: [Laptop ($1500.0), Smartphone ($800.0), Tablet ($600.0)]
Conclusion
The Collections.copy() method is a powerful utility for copying elements between lists while maintaining their order. It is particularly useful for synchronizing list contents or creating clones of lists. By understanding and using copy(), you can effectively manage and manipulate list data in your Java applications, ensuring that collections remain consistent and accurate.