Collections copy() method in Java with Examples
The copy() method of java.util.Collections class is used to copy all of the elements from one list into another.
After the operation, the index of each copied element in the destination list will be identical to its index in the source list. The destination list must be at least as long as the source list. If it is longer, the remaining elements in the destination list are unaffected.
This method runs in linear time.
Syntax:
public static void copy(List dest, List src)
Parameters: This method takes the following argument as a parameter
- dest – The destination list.
- src – The source list.
Exception: This method throws IndexOutOfBoundsException, if the destination list is too small to contain the entire source List.
Below are the examples to illustrate the checkedSortedSet() method
Example 1:
import java.util.*;
public class GFG1 {
public static void main(String[] args)
throws Exception
{
try {
List<String> srclst = new ArrayList<String>( 3 );
List<String> destlst = new ArrayList<String>( 3 );
srclst.add( "Ram" );
srclst.add( "Gopal" );
srclst.add( "Verma" );
destlst.add( "1" );
destlst.add( "2" );
destlst.add( "3" );
System.out.println( "Value of source list: " + srclst);
System.out.println( "Value of destination list: " + destlst);
System.out.println( "\nAfter copying:\n" );
Collections.copy(destlst, srclst);
System.out.println( "Value of source list: " + srclst);
System.out.println( "Value of destination list: " + destlst);
}
catch (IllegalArgumentException e) {
System.out.println( "Exception thrown : " + e);
}
catch (IndexOutOfBoundsException e) {
System.out.println( "Exception thrown : " + e);
}
}
}
|
Output:
Value of source list: [Ram, Gopal, Verma]
Value of destination list: [1, 2, 3]
After copying:
Value of source list: [Ram, Gopal, Verma]
Value of destination list: [Ram, Gopal, Verma]
Example 2: For IndexOutOfBoundsException
import java.util.*;
public class GFG1 {
public static void main(String[] argv) throws Exception
{
try {
List<String> srclst = new ArrayList<String>( 3 );
List<String> destlst = new ArrayList<String>( 2 );
srclst.add( "Ram" );
srclst.add( "Gopal" );
srclst.add( "Verma" );
destlst.add( "1" );
destlst.add( "2" );
System.out.println( "Value of source list: " + srclst);
System.out.println( "Value of destination list: " + destlst);
System.out.println( "\nAfter copying:\n" );
Collections.copy(destlst, srclst);
System.out.println( "Value of source list: " + srclst);
System.out.println( "Value of destination list: " + destlst);
}
catch (IllegalArgumentException e) {
System.out.println( "Exception thrown : " + e);
}
catch (IndexOutOfBoundsException e) {
System.out.println( "Exception thrown : " + e);
}
}
}
|
Output:
Value of source list: [Ram, Gopal, Verma]
Value of destination list: [1, 2]
After copying:
Exception thrown :
java.lang.IndexOutOfBoundsException:
Source does not fit in dest
Last Updated :
15 May, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...