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()
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()
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]