Home » Java programming language

Java Collections reverseOrder() Method with Example

Collections Class reverseOrder() method: Here, we are going to learn about the reverseOrder() method of Collections Class with its syntax and example.
Submitted by Preeti Jain, on February 08, 2020

Collections Class reverseOrder() method

Syntax:

    public static Comparator reverseOrder();
    public static Comparator reverseOrder(Comparator com);
  • reverseOrder() method is available in java.util package.
  • reverseOrder() method is used to reverse the order of collection object based on default or natural ordering.
  • reverseOrder(Comparator com) method is used to reverse the order of collection object based on defined Comparator object.
  • These methods don't throw an exception at the time of defining the order.
  • These are static methods and it is accessible with the class name and if we try to access these methods with the class object then also we will not get any error.

Parameter(s):

  • In the first case, reverseOrder(),
    • It does not accept any parameter.
  • In the first case, reverseOrder(Comparator com),
    • Comparator com – represents the Comparator object.

Return value:

In both the cases, the return type of the method is Comparator, it returns the greatest value element of the given collection depend upon the given Comparator.

  • reverseOrder() – returns Comparator object based on default ordering.
  • reverseOrder(Comparator com) – returns Comparator object based on defined Comparator object.

Example:

// Java program to demonstrate the example 
// of reverseOrder() method of Collections

import java.util.*;

public class ReverseOrderOfCollections {
    public static void main(String args[]) {
        // Instantiates an array list object
        List < Integer > arr_l = new ArrayList < Integer > ();

        // By using add() method is to add
        //objects in an array list
        arr_l.add(20);
        arr_l.add(10);
        arr_l.add(40);
        arr_l.add(30);
        arr_l.add(50);

        // Display ArrayList
        System.out.println("ArrayList: " + arr_l);

        // By using reverseOrder() method is to reverse
        // the order of list based on default ordering
        Comparator default_cmp = Collections.reverseOrder();
        Collections.sort(arr_l, default_cmp);

        // Display ArrayList based on default comparator
        System.out.println("Collections.sort(arr_l,default_cmp): " + arr_l);

        // By using reverseOrder(comparator) method is to reverse
        // the order of list based on defined or 
        // customized comparator object

        Comparator customized_cmp = Collections.reverseOrder(null);
        Collections.sort(arr_l, default_cmp);

        // Display ArrayList based on customized comparator
        System.out.println("Collections.sort(arr_l,customized_cmp): " + arr_l);
    }
}

Output

ArrayList: [20, 10, 40, 30, 50]
Collections.sort(arr_l,default_cmp): [50, 40, 30, 20, 10]
Collections.sort(arr_l,customized_cmp): [50, 40, 30, 20, 10]






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.