Thursday, 9 July 2015

Java : Collection Framework : Collections (ReverseOrder Comparator null)

Click here to watch in Youtube :
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;


 public static <T> Comparator<T> reverseOrder(Comparator<T> cmp)


 cmp - a comparator who's ordering is to be reversed by the returned comparator or null


 A comparator that imposes the reverse ordering of the specified comparator.


public class CollectionsExample

    public static void main(String[] args)

        LinkedList<Integer> linkedList = new LinkedList<Integer>();


        System.out.println("linkedList before Sort " + linkedList+"\n");

         * Returns a comparator that imposes the reverse ordering of the
         * specified comparator. If the specified comparator is null, this
         * method is equivalent to reverseOrder() (in other words, it returns a
         * comparator that imposes the reverse of the natural ordering on a
         * collection of objects that implement the Comparable interface).
        Comparator<Integer> reverseOrderComparator = Collections.reverseOrder(null);

        // sort the list
        Collections.sort(linkedList, reverseOrderComparator);

        System.out.println("linkedList sorted in ReverseOrder: " + linkedList);

linkedList before Sort [2, 1, 4, 3]

linkedList sorted in ReverseOrder: [4, 3, 2, 1]
To Download CollectionsDemo-ReverseOrderNullComparator Project Click the below link

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

    Google+ Followers

    Google+ Badge