Home » Java programming language

Differences between the reverse() and reverseOrder() in Java

reverse() vs reverseOrder() in Java: Here, we are going to learn what are the differences between the reverse() and reverseOrder() in Java programming language?
Submitted by Preeti Jain, on August 08, 2019

reverse() vs reverseOrder() in Java

Here, we will see how reverse() method differs from reverseOrder() in Java?

reverse()

  • This method is available in java.util package.
  • This method is static so this method is accessible with the class name too.
  • With the help of reverse() method to reverse the elements of Collection.
  • This method takes one argument of the method of Collections class and the argument will be Collection object List.
  • This return type of this method is void so it does not return anything.
  • The syntax of this method is given below:
    public static void reverse(List obj){}

Example:

// Java program to demonstrate the behavior of reverse() method

import java.util.Collections;
import java.util.LinkedList;

class CollectionsReverseMethod {
    public static void main(String[] args) {

        // Creating an instance of LinkedList
        LinkedList ll = new LinkedList();

        // By using add() method to add few elements in a lits
        ll.add(10);
        ll.add(30);
        ll.add(70);
        ll.add(60);
        ll.add(50);

        // Display Current Linked List
        System.out.println("Current List is :" + ll);

        // Implement reverse() method
        Collections.reverse(ll);

        // Display New Reversed Linked List
        System.out.println("New Reversed List is " + ll);
    }
}

Output

E:\Programs>javac CollectionsReverseMethod.java

E:\Programs>java CollectionsReverseMethod
Current List is :[10, 30, 70, 60, 50]
New Reversed List is [50, 60, 70, 30, 10]

Here, we will see how reverseOrder() method differs from reverse() in Java?


reverseOrder()

  • This method is available in java.util package.
  • This method is also a static method so this method is accessible with the class name too.
  • With the help of the reverseOrder() method to reverse the original sorting order and return Comparator.
  • This method is an overloaded method. The two flavors are available of this method first reverseOrder() and second, reverseOrder(Comparator c).
  • This reverseOrder() does not take any argument of the method and return Comparator and this method is applicable for the natural order of reversing that implement the Comparable interface.
  • This reverseOrder(Comparator) takes only one argument of the method and return Comparator and this method is applicable for the order of reversing of the given Comparator.
  • The return type of this method is Comparator so it returns Comparator.
  • The syntax of this method is given below:
    public static Comparator reverseOrder()
    public static Comparator reverseOrder(Comparator comp)

Example:

// Java program to demonstrate the behavior of reverseOrder() method

import java.util.Collections;
import java.util.LinkedList;
import java.util.Comparator;

class CollectionsReverseOrderMethod {
    public static void main(String[] args) {
        // Creating an instance of LinkedList
        LinkedList ll = new LinkedList();

        // By using add() method to add few elements in a lits
        ll.add(10);
        ll.add(30);
        ll.add(70);
        ll.add(60);
        ll.add(50);

        // Display Current Linked List
        System.out.println("Current List is :" + ll);

        // creates a comparator object and then reverse
        Comparator comp =
            Collections.reverseOrder(null);

        Collections.sort(ll, comp);

        // Display New Reversed Linked List
        System.out.println("New Reversed List is given below " + ll);
    }
}

Output

E:\Programs>javac CollectionsReverseOrderMethod.java

E:\Programs>java CollectionsReverseOrderMethod
Current List is :[10, 30, 70, 60, 50]
New Reversed List is given below [70, 60, 50, 30, 10]


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.