Home > Java > javaTutorial > body text

copy() in Java

WBOY
Release: 2024-08-30 15:38:30
Original
960 people have browsed it

In Java, while using collections, we come across various scenarios to copy one list’s elements into the other with a precaution that the index of elements must not be affected while copying. For such type of scenarios, java.util.Collections package provides us with a function copy() that copies elements present in one list into the other list, such that elements have the same index in the new list as well. While using this function, the destination list’s size must be greater than the size of the source list. This time complexity of this function is linear.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax with Parameters

Following is a syntax of copy() in java with parameters:

Syntax:

public static void copy(List dest, List src)
Copy after login
  • Here, the public keyword specifies that this function can be easily called in any other class.
  • Static keyword refers that this function works on class level and thus need will be called by other static functions only as well as can be called using a class name not using the object of the class.
  • Void keyword is used to indicate this function returns nothing.

Arguments:

  • dest: It is a List type variable that holds the reference for the destination list where data needs to be copied.
  • src: It is a List type variable that holds the reference for the source list that needs to be copied.

Above function throws below exception:

  • IndexOutOfBoundsException: This exception occurs when the destination list’s size is smaller than the size of the source list.
  • UnsupportedOperationException: This exception occurs when the list iterator of the destination list does not support the set operation.

How copy() method works in Java?

  • Copy() method takes 2 lists. Source list whose elements need to be copied and destination list where the elements of the list need to be copied.
  • First, it is checked if the destination list’s size is greater than the source list; otherwise, the IndexOutOfBoundsException exception is thrown.
  • This indicates that JVM is unable to find the index for the element need to be copied because while copying the element from the source list to the destination list, the index of the element is also taken care of. If in case there is one element present in the destination list at the same index, the particular element is overwritten by the copying element.
  • Else elements from the source list are copied one by one with their indexes’ value and gets copied to the destination list at the same indexes. The elements present at other indexes in the destination list remain unchanged.
  • This whole operation is done using a single iteration of elements, thus have linear time complexity that means complexity is O(n).

Examples of copy() in Java

Given below are the examples mentioned below:

Example #1

To illustrate the occurrence of IndexOutOfBoundsException when the size of the destination list is less than the source list.

Code:

import java.util.*;
public class Demo {
public static void main(String[] args) {
List<String> mysrclist = new ArrayList<String>(6);
List<String> mydestlist = new ArrayList<String>(11);
mysrclist.add("Lets practice");
mysrclist.add("Java");
mysrclist.add("with US");
mysrclist.add("EDUCBA");
mysrclist.add("is a great");
mysrclist.add("Learnig Platform");
Collections.copy(mydestlist, mysrclist);
System.out.println("Elements of source list: "+mysrclist);
System.out.println("Elements of destination list: "+mydestlist);
}
}
Copy after login

Output:

Since the destination list size is 0 and the size of the source list is 6, there is no place in the destination list at that particular index; thus, this below exception is thrown as an output.

copy() in Java

Explanation:

  • In the case of the above program, 2 lists are taken and sent to the copy() method as arguments, but since the size of the destination list is smaller than the size of the source list, thus, IndexOutOfBoundsException occurs.
  • Such an issue is solved in the below example, where the size of the destination list is the same as the source list.

Example #2

Code:

import java.util.*;
public class Demo {
public static void main(String[] args) {
List<String> mysrclist = new ArrayList<String>(6);
List<String> mydestlist = new ArrayList<String>(11);
mysrclist.add("Lets practice");
mysrclist.add("Java");
mysrclist.add("with US");
System.out.println("Elements of source list: "+mysrclist);
mydestlist.add("EDUCBA");
mydestlist.add("is a great");
mydestlist.add("Learnig Platform");
System.out.println("Elements of destination list before copy() executes: "+mydestlist);
Collections.copy(mydestlist, mysrclist);
System.out.println("Elements of destination list after copy() executes: "+mydestlist);       }
}
Copy after login

Output:

copy() in Java

Explanation:

  • The source list has 3 elements at 0,1 and 2 index the same as the destination list in the above example.
  • When these 2 lists are passed in the copy function, since the size of the 2 lists are the same, elements in the destination list get overwritten by the source list elements at the same indexes.

Example #3

In this example, we will see how the destination list elements get overwritten when elements of the source list get copied into it.

Code:

import java.util.*;
public class Demo {
public static void main(String[] args) {
//Create lists for source and destination
List<String> mysrclist = new ArrayList<String>(6);
List<String> mydestlist = new ArrayList<String>(11);
//Populate two source and destination lists
mysrclist.add("DIgital Learning");
mysrclist.add("for Java");
mysrclist.add("is great");
System.out.println("Elements of source list: "+mysrclist);
mydestlist.add("Book Learning");
mydestlist.add("is");
mydestlist.add("a great");
mydestlist.add("Learnig Platform");
mydestlist.add("For All");
System.out.println("Elements of destination list before copying : "+mydestlist);
// copy into destination list
Collections.copy(mydestlist, mysrclist);
System.out.println("Elements of destination list after copying : "+mydestlist);
}
}
Copy after login

Output:

copy() in Java

Explanation:

  • Here we saw the elements at the first 3 indexes in the destination list get overwritten when elements are copied into it, and elements at other indexes remain unaffected.

Conclusion

The collections package provides us with a public static method that can be used to copy one source list’s elements into the destination list at the same indexes. This method works only in the destination list, which is greater than the source list, and this operation has linear time complexity.

The above is the detailed content of copy() in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template