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
  • Java : Collection Framework : Collections (addAll Hashset)


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

    CollectionsExample.java
    import java.util.Collections;
    import java.util.HashSet;
    
    /*
     *  Example of addAll(Collection<? super T> c,
                                        T... elements) method
     */
    
    public class CollectionsExample
    {
    
        public static void main(String[] args)
        {
            HashSet<String> hashSet = new HashSet<String>();
    
            hashSet.add("Ajay");
            hashSet.add("Vijay");
            hashSet.add("David");
    
            System.out.println("hashSet : " + hashSet + "\n");
    
            String[] namesArray =
            { "Ajay", "Karan", "Julia" };
    
            /*
             * Adds all of the specified elements to the specified collection.
             * Elements to be added may be specified individually or as an array
             * 
             * Returns: true if the collection changed as a result of the call
             */
            boolean isAdded = Collections.addAll(hashSet, namesArray);
    
            System.out.println("isAdded : " + isAdded + "\n");
    
            System.out.println("hashSet : " + hashSet + "\n");
        }
    
    }
    
    Output
    hashSet : [Vijay, David, Ajay]
    
    isAdded : true
    
    hashSet : [Julia, Vijay, Karan, David, Ajay]
    
    To Download CollectionsDemoAddAllHashSet Project Click the below link
    https://sites.google.com/site/javaee4321/java-collections/CollectionsDemoAddAllHashSet.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
  • Java : Collection Framework : Collections (addAll)


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

    CollectionsExample.java
    import java.util.ArrayList;
    import java.util.Collections;
    
    /*
     *  Example of addAll(Collection<? super T> c,
                                    T... elements) method
     */
    
    public class CollectionsExample
    {
    
        public static void main(String[] args)
        {
            ArrayList<String> arrayList = new ArrayList<String>();
    
            arrayList.add("Ajay");
            arrayList.add("Vijay");
            arrayList.add("David");
    
            System.out.println("arrayList : " + arrayList+"\n");
    
            /*
             * Adds all of the specified elements to the specified collection.
             * Elements to be added may be specified individually or as an array
             * 
             * Returns: true if the collection changed as a result of the call
             */
            boolean isAdded = Collections.addAll(arrayList, "Raju", "Karan", "Julia");
            
            System.out.println("isAdded : "+isAdded+"\n");
    
            System.out.println("arrayList : " + arrayList+"\n");
    
        }
    
    }
    
    Output
    arrayList : [Ajay, Vijay, David]
    
    isAdded : true
    
    arrayList : [Ajay, Vijay, David, Raju, Karan, Julia]
    
    To Download CollectionsDemoAddAllApp Project Click the below link
    https://sites.google.com/site/javaee4321/java-collections/CollectionsDemoAddAllApp.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
  • Java : Collection Framework : Collections (BinarySearch)


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

    CollectionsExample.java
    import java.util.ArrayList;
    import java.util.Collections;
    
    /*
     *  Example of  
     *  binarySearch(List<? extends Comparable<? super T>> list,T key) method
     */
    
    public class CollectionsExample
    {
    
        public static void main(String[] args)
        {
            ArrayList<String> arrayList = new ArrayList<String>();
    
            arrayList.add("Ajay");
            arrayList.add("Vijay");
            arrayList.add("David");
            arrayList.add("Raju");
            arrayList.add("Karan");     
    
            System.out.println("arrayList : " + arrayList+"\n");
    
            /*
             * Searches the specified list for the specified object using the binary
             * search algorithm. The list must be sorted into ascending order
             * according to the natural ordering of its elements (as by the
             * sort(List) method) prior to making this call. If it is not sorted,
             * the results are undefined. If the list contains multiple elements
             * equal to the specified object, there is no guarantee which one will
             * be found.
             * 
             * Returns: the index of the search key, if it is contained in the list
             */
            int indexPostion = Collections.binarySearch(arrayList, "Raju");
            
            System.out.println("indexPostion : "+indexPostion+"\n");        
    
        }
    
    }
    
    Output
    arrayList : [Ajay, Vijay, David, Raju, Karan]
    
    indexPostion : 3
    
    To Download CollectionsDemoBinarySearchApp Project Click the below link
    https://sites.google.com/site/javaee4321/java-collections/CollectionsDemoBinarySearchApp.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
  • Java : Collection Framework : Collections (Sort LinkedList)


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

    CollectionsSortExample.java
    import java.util.Collections;
    import java.util.LinkedList;
    
    /*
     *  Example of sort(List<T> list) method.
     */
    
    public class CollectionsSortExample
    {
    
        public static void main(String[] args)
        {
            LinkedList<String> linkedList = new LinkedList<String>();
            linkedList.add("Dog");
            linkedList.add("Ball");
            linkedList.add("Cat");
            linkedList.add("Apple");
    
            System.out.println("Before Sorting  : " + linkedList + "\n");
    
            /*
             * Sorts the specified list into ascending order, according to the
             * natural ordering of its elements.
             * 
             * All elements in the list must implement the Comparable interface.
             */
            Collections.sort(linkedList);
    
            System.out.println("After Sorting   : " + linkedList);
    
        }
    
    }
    
    Output
    Before Sorting  : [Dog, Ball, Cat, Apple]
    
    After Sorting   : [Apple, Ball, Cat, Dog]
    
    To Download CollectionsSortDemoLinkedListApp Project Click the below link
    https://sites.google.com/site/javaee4321/java-collections/CollectionsSortDemoLinkedListApp.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
  • Java : Collection Framework : Collections (Sort Double)


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

    CollectionsSortExample.java
    import java.util.ArrayList;
    import java.util.Collections;
    
    /*
     *  Example of sort(List<T> list) method
     */
    
    public class CollectionsSortExample
    {
    
        public static void main(String[] args)
        {
            ArrayList<Double> arrayList = new ArrayList<Double>();
            arrayList.add(1.4);
            arrayList.add(1.2);
            arrayList.add(1.3);
            arrayList.add(1.1);
    
            System.out.println("Before Sorting  : " + arrayList + "\n");
    
            /*
             * Sorts the specified list into ascending order, according to the
             * natural ordering of its elements.
             * 
             * All elements in the list must implement the Comparable interface.
             */
            Collections.sort(arrayList);
    
            System.out.println("After Sorting   : " + arrayList);
    
        }
    
    }
    
    Output
    Before Sorting  : [1.4, 1.2, 1.3, 1.1]
    
    After Sorting   : [1.1, 1.2, 1.3, 1.4]
    
    To Download CollectionsSortDemoSortDoubleApp Project Click the below link
    https://sites.google.com/site/javaee4321/java-collections/CollectionsSortDemoSortDoubleApp.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
  • Java : Collection Framework : Collections (sort String)


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

    CollectionsSortExample.java
    import java.util.ArrayList;
    import java.util.Collections;
    
    public class CollectionsSortExample
    {
    
        public static void main(String[] args)
        {
            ArrayList<String> arrayList = new ArrayList<String>();
            arrayList.add("Dog");
            arrayList.add("Ball");
            arrayList.add("Cat");
            arrayList.add("Apple");
    
            System.out.println("Before Sorting  : " + arrayList + "\n");
    
            /*
             * Sorts the specified list into ascending order, according to the
             * natural ordering of its elements.
             * 
             * All elements in the list must implement the Comparable interface.
             */
            Collections.sort(arrayList);
    
            System.out.println("After Sorting   : " + arrayList);
    
        }
    
    }
    
    Output
    Before Sorting  : [Dog, Ball, Cat, Apple]
    
    After Sorting   : [Apple, Ball, Cat, Dog]
    
    To Download CollectionsSortDemoSortStringApp Project Click the below link
    https://sites.google.com/site/javaee4321/java-collections/CollectionsSortDemoSortStringApp.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
  • Java : Collection Framework : Collections (sort)


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

    CollectionsSortExample.java
    import java.util.ArrayList;
    import java.util.Collections;
    
    /*
     *  Example of sort(List<T> list) method
     */
    
    public class CollectionsSortExample
    {
    
        public static void main(String[] args)
        {
            ArrayList<Integer> arrayList = new ArrayList<Integer>();
            arrayList.add(400);
            arrayList.add(200);
            arrayList.add(100);
            arrayList.add(300);
    
            System.out.println("Before Sorting  : " + arrayList + "\n");
    
            /*
             * Sorts the specified list into ascending order, according to the
             * natural ordering of its elements.
             * 
             * All elements in the list must implement the Comparable interface.
             */
            Collections.sort(arrayList);
    
            System.out.println("After Sorting   : " + arrayList);
    
        }
    
    }
    
    Output
    Before Sorting  : [400, 200, 100, 300]
    
    After Sorting   : [100, 200, 300, 400]
    
    To Download CollectionsSortDemoSortApp Project Click the below link
    https://sites.google.com/site/javaee4321/java-collections/CollectionsSortDemoSortApp.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