Java Collections reverseOrder() and reverse() Example

By Arvind Rai, November 12, 2023
On this page, I will provide Java Collections.reverseOrder() and Collections.reverse() method example. The reverseOrder() method returns Comparator that reverses the elements ordering imposed by specified Comparable and Comparator for the collection object. The reverse() method just reverses the elements ordering in a List.
Find the examples.

1. Collections.reverseOrder with Comparable

It returns a Comparator that will reverse the natural ordering imposed by Comparable.
ReverseOrderComparableDemo.java
package com.concretepage.util;
import java.util.Arrays;
import java.util.Collections;

public class ReverseOrderComparableDemo {
    public static void main(String[] args) {
        User u1 = new User("Mohan");
        User u2 = new User("Sohan");
        User u3 = new User("Rahim");
        User[] users = {u1, u2, u3};
        System.out.println("...Natural Order...");
        Arrays.sort(users);
        for(User u: users){
            System.out.print(u.getName()+" ");
        }
        System.out.println("\n...Reverse Order...");
        Arrays.sort(users, Collections.reverseOrder());
        for(User u: users){
        	System.out.print(u.getName()+" ");
        }
    }
} 
User.java
package com.concretepage.util;
public class User implements Comparable<User>  {
        private String name;
        public User(String name) {
                this.name = name;
        }
        public String getName() {
		return name;
        }
	    @Override
	    public int compareTo(User o) {
		   return name.compareTo(o.getName());
	    }
} 
Output
...Natural Order...
Mohan Rahim Sohan 
...Reverse Order...
Sohan Rahim Mohan  

2. Collections.reverseOrder with Comparator

It returns a Comparator that will reverse the natural ordering imposed by Comparator.
ReverseOrderComparatorDemo.java
package com.concretepage.util;
import java.util.Arrays;
import java.util.Collections;
public class ReverseOrderComparatorDemo {
    public static void main(String[] args) {
        User u1 = new User("Mohan");
        User u2 = new User("Sohan");
        User u3 = new User("Rahim");
        User[] users = {u1, u2, u3};
        System.out.println("...Order with Comparator...");
        Arrays.sort(users, new UserComparator());
        for(User u: users){
            System.out.print(u.getName()+" ");
        }
        System.out.println("\n...Reverse Order with Comparator...");
        Arrays.sort(users, Collections.reverseOrder(new UserComparator()));
        for(User u: users){
        	System.out.print(u.getName()+" ");
        }
    }
} 
UserComparator.java
package com.concretepage.util;
import java.util.Comparator;
public class UserComparator implements Comparator<User> {
	@Override
	public int compare(User o1, User o2) {
		return o1.getName().compareTo(o2.getName());
	}
} 
Output
...Order with Comparator...
Mohan Rahim Sohan 
...Reverse Order with Comparator...
Sohan Rahim Mohan  

3. Collections.reverse

It simply reverses the order of List elements.
ReverseDemo.java
package com.concretepage.util;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class ReverseDemo {
    public static void main(String[] args) {
        User u1 = new User("Mohan");
        User u2 = new User("Sohan");
        User u3 = new User("Rahim");
        List<User> list = Arrays.asList(u1, u2, u3);
        System.out.println("...Before Reverse...");
        for(User u: list){
            System.out.print(u.getName()+" ");
        }
        System.out.println("\n...After Reverse...");
        Collections.reverse(list);
        for(User u: list){
            System.out.print(u.getName()+" ");
        }
    }
} 
Output
...Before Reverse...
Mohan Sohan Rahim 
...After Reverse...
Rahim Sohan Mohan  
POSTED BY
ARVIND RAI
ARVIND RAI









©2023 concretepage.com | Privacy Policy | Contact Us