Monday 29 June 2015

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
  • No comments:

    Post a Comment