Monday, 29 June 2015

Java : Collection Framework : Collections (Copy)


Click here to watch in Youtube :
https://www.youtube.com/watch?v=CK0efUCV3Xg&list=UUhwKlOVR041tngjerWxVccw

CollectionsExample.java
import java.util.ArrayList;
import java.util.Collections;

/*
 *  Example of  
 *  copy(List<? super T> dest,List<? extends T> src) method
 */

public class CollectionsExample
{

    public static void main(String[] args)
    {

        ArrayList<Integer> sourceList = new ArrayList<Integer>();

        sourceList.add(1000);
        sourceList.add(2000);
        sourceList.add(3000);
        sourceList.add(4000);

        System.out.println("sourceList : " + sourceList + "\n");

        ArrayList<Integer> destinationList = new ArrayList<Integer>();

        destinationList.add(10);
        destinationList.add(20);
        destinationList.add(30);
        destinationList.add(40);

        System.out.println("destinationList : " + destinationList + "\n");

        /*
         * Copies 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.
         */

        Collections.copy(destinationList, sourceList);

        System.out.println("destinationList After Copy: " + destinationList);

    }

}
Output
sourceList : [1000, 2000, 3000, 4000]

destinationList : [10, 20, 30, 40]

destinationList After Copy: [1000, 2000, 3000, 4000]
To Download CollectionsDemoCopyApp Project Click the below link
https://sites.google.com/site/javaee4321/java-collections/CollectionsDemoCopyApp.zip?attredirects=0&d=1

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • No comments:

    Post a Comment