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:
// Java program to demonstrate // copy() method import java.util.*; public class GFG1 { public static void main(String[] args) throws Exception { try { // creating object of Source list and destination List List<String> srclst = new ArrayList<String>(3); List<String> destlst = new ArrayList<String>(3); // Adding element to srclst srclst.add("Ram"); srclst.add("Gopal"); srclst.add("Verma"); // Adding element to destlst destlst.add("1"); destlst.add("2"); destlst.add("3"); // printing the srclst System.out.println("Value of source list: " + srclst); // printing the destlst System.out.println("Value of destination list: " + destlst); System.out.println("\nAfter copying:\n"); // copy element into destlst Collections.copy(destlst, srclst); // printing the srclst System.out.println("Value of source list: " + srclst); // printing the destlst 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); } } } |
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
// Java program to demonstrate // copy() method import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { // creating object of Source list and destination List List<String> srclst = new ArrayList<String>(3); List<String> destlst = new ArrayList<String>(2); // Adding element to srclst srclst.add("Ram"); srclst.add("Gopal"); srclst.add("Verma"); // Adding element to destlst destlst.add("1"); destlst.add("2"); // printing the srclst System.out.println("Value of source list: " + srclst); // printing the destlst System.out.println("Value of destination list: " + destlst); System.out.println("\nAfter copying:\n"); // copy element into destlst Collections.copy(destlst, srclst); // printing the srclst System.out.println("Value of source list: " + srclst); // printing the destlst 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); } } } |
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
Recommended Posts:
- Collections min() method in Java with Examples
- Collections max() method in Java with Examples
- Collections synchronizedList() method in Java with Examples
- Collections synchronizedMap() method in Java with Examples
- Collections unmodifiableCollection() method in Java with Examples
- Collections unmodifiableSortedMap() method in Java with Examples
- Collections synchronizedSortedSet() method in Java with Examples
- Collections synchronizedSortedMap() method in Java with Examples
- Collections unmodifiableMap() method in Java with Examples
- Collections unmodifiableSet() method in Java with Examples
- Collections synchronizedSet() method in Java with Examples
- Collections swap() method in Java with Examples
- Collections fill() method in Java with Examples
- Collections singletonMap() method in Java with Examples
- Collections synchronizedCollection() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai



