Collections.reverseOrder() in Java with Examples


Collections reverse order class is a reverse order method, which is encoded in the collections class. It is present in the java.util package. It returns a comparator as a result, which is a predefined comparator in nature. By using this comparator package, we can rearrange the collections of a particular data set in a reverse manner. There are two different variants of the Java reverseOrder(), which can be defined as per the capacity of their parameters. Those are −

  • Java Collections reverseOrder() Method − is used to get the value of a comparator class which can impose the reverse manner on the previous order of a data set. It also implement a comparable interface to run this particular method.

  • Java Collections reverseOrder(comp) Method − is used to get the value of a comparator class of a particular data set, with some specified comparator.

public static Comparator reverseOrder()
Or;
public static <T> Comparator<T> reverseOrder()

To implement a comparaor class, we need to keep in mind some important aspects.

  • Parameter − For the comparator class, the ordering will be reversed with some return value. The value can be null in nature also.

  • Return value type − User will have a return value, when we will apply the comparable interface class on the previous ordered collection.

Algorithm of a Collections.reverseOrder() Class

In this possible algorithm, we are going to show you how to perform the sorting process to get the reverse order of a particular array list by using collections reverse order method.

  • Step 1 − Start the process

  • Step 2 − Import and define some Java classes

  • Step 3 − Declare and construct a class with class name 1

  • Step 4 − Mention a helper class

  • Step 5 − Declare the attributes

  • Step 6 − Mention a costructor class

  • Step 7 − Show the public keywords to refer some current instance

  • Step 8 − Declare the main method

  • Step 9 − Return the value for class 1

  • Step 10 − Repeat the steps from 3 to 9 for next classes as you want

  • Step 11 − Create an empty array list

  • Step 12 − Add and populate the total data set

  • Step 13 − Write a display text for the unsorted values

  • Step 14 − Iterate the method

  • Step 15 − Sort the list with some different parameters

  • Step 16 − Display the sorted text

  • Step 17 − Iterate the loop

  • Step 18 − Sorting the data set by the desending order

  • Step 19 − Return the value

  • Step 20 − Terminate the process

Syntax of a Collections.reverseOrder() class

public class NAME {
	public static void main(String args[]) {
		//Declare the function and create a linked list object
		LinkedList<Integer> list = new LinkedList<Integer>();
		//Input some data into it
		list.add(7);
		list.add(16);
		list.add(10);
		list.add(-15);
	//Declare and create a comparator class to run the method of a reverse order
		Comparator<Integer> cmp = Collections.reverseOrder();
		//Perform the process of sorting with the particular list
		Collections.sort(list, cmp);
		System.out.println("List sorted in the ReverseOrder manner. Have a look:
		");
	for(int i : list) {
		System.out.println(i+ " ");
}

In this possible syntax above, we have tried to show you how to declare and create a collection class and get the reverse value of it by using the Collections reverse order method. By using this particular syntax, we are heading towards some possible approaches related to the problem statement.

Approaches to Follow

  • Approach 1 − Java Program to Illustrate The Working of a reverseOrder() method of the Collections class To sort a list by using the manner of descending order

  • Approach 2 − Java Program to demonstrate the working of a reverseOrder(Comparator c) to sort the students name in descending order on the basis of roll numbers when there is a user defined comparator to perform the reverse operation

Approach 1: to Sort a List in Descending Order

Use of ReverseOder() Method

In this possible approach, we are going to show you how to perform the sorting process with a list to get the desired output in a descending order. But in this particular method, we can not use the sort() method on an array directly to refine the elements in the sescending manner. This method work fine with the Array Objects, such as Integer values but it is not for the int array also know as primitive array . If we try to do that, the process will return an exception as −

no suitable method has been found for sort (Array List <Integer>, Comparator
<Object>)
Arrays . sort (al, Collections . reverse Order());
It is not acceptable

Example 1

//Java Program to Illustrate The Working of a reverseOrder() method of the Collections class To sort a list by using the manner of descending order
import java.util.*;
public class ARBRDD {
   public static void main(String[] args){
      ArrayList<Integer> al = new ArrayList<Integer>();
      al.add(07);
      al.add(16);
      al.add(10);
      al.add(40);
      al.add(50);
      Collections.sort(al, Collections.reverseOrder());
      System.out.println(
         "List after the use of Collection.reverseOrder()"
         + " and Collections.sort() :\n" + al);
   }
}

Output

List after the use of Collection.reverseOrder() and Collections.sort() :
[50, 40, 16, 10, 7]

In this particular Java approach, we are going to construct and use the collections class to sort an array in the descending manner. First, we are going to apply the collection sort method for the process and array sort() function for further. It is a reverse sorting manner where we also use the Arrays.toString(arr) method for the desired output.

Example 2

//Java Program to Demonstrate Working of reverseOrder() to Sort an Array in Descending Order
import java.util.*;
public class ARBRDD {
   public static void main(String[] args){
      Integer[] arr = { 30, 07, 16, 10 };
      Arrays.sort(arr, Collections.reverseOrder());
      System.out.println(
         "Array after the use of Collection.reverseOrder()"
         + " and Arrays.sort() :\n"
         + Arrays.toString(arr));
   }
}

Output

Array after the use of Collection.reverseOrder() and Arrays.sort() :
[30, 16, 10, 7]

Approach 2: to Sort Array Elements in Descending Order

Use of ReverseOrder(Comparator c) Method

In this approach, we will use the comparator class to sort the data elements from a student data source by using the order of the roll numbers in a reverse manner. For this the pseudo notation is −

public static Comparator reverseOrder(Comparator c)

The process will return a comparator as value which can impose the value of a reverse order as a passed comparator object class which is an user defiend comparator class.

Example

// Java Program to demonstrate the working of a reverseOrder(Comparator c) to sort the students name in descending order on the basis of roll numbers when there is a user defined comparator to perform the reverse operation
import java.io.*;
import java.lang.*;
import java.util.*;
class Student {
   int rollno;
   String name, address;
   public Student(int rollno, String name, String address){
      this.rollno = rollno;
      this.name = name;
      this.address = address;
   }
   public String toString(){
      return this.rollno + " " + this.name + " "
      + this.address;
   }
}
class Sortbyroll implements Comparator<Student> {
   public int compare(Student a, Student b){
      return a.rollno - b.rollno;
   }
}
public class ARBRDD {
   public static void main(String[] args){
      ArrayList<Student> ar = new ArrayList<Student>();
      ar.add(new Student(1610, "RDD", "KOL"));
      ar.add(new Student(131, "ARB", "DHAKA"));
      ar.add(new Student(121, "AGB", "GERMANY"));
      System.out.println("Unsorted List");
      for (int i = 0; i < ar.size(); i++)
      System.out.println(ar.get(i));
      Comparator c
      = Collections.reverseOrder(new Sortbyroll());
      Collections.sort(ar, c);
      System.out.println("\nSorted by the rollno");
      for (int i = 0; i < ar.size(); i++)
      System.out.println(ar.get(i));
   }
}

Output

Unsorted List
1610 RDD KOL
131 ARB DHAKA
121 AGB GERMANY
Sorted by the rollno
1610 RDD KOL
131 ARB DHAKA
121 AGB GERMANY

Conclusion

The reverse operation is a comparator based process, which is performed by the reverseOrder() method present in a Java environment by using a comparable interface on a data set. Today in this article we have learned about the process process of reversal of an array by using a particular comparator class. With the above mentioned logics, syntax and algoritm; we have tried to build come Java codes to solve the problem statement in an efficient manner.

Updated on: 27-Dec-2023

42 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements