Monday, March 8, 2021

Sorting ArrayList in Reverse or Descending Order in Java 8

Oracle Java Exam Prep, Oracle Java Tutorial and Material, Oracle Java Preparation, Oracle Java 8

A quick guide on how to sort the arraylist in descending order or reverse order in java and example programs using Collections.reverseOrder() method.

1. Overview

In this article, We will learn how to sort ArrayList in descending order in java. Sometimes this is referred as collections reverse or decreasing order.

To get the list in the reverse order, we need to use Collections.reverseOrder() and Collections.sort() methods together.

2.  Sort ArrayList In Descending order using Collections.reverseOrder()

In the below examples, we are using the built-in comparator from the reverseOrder() method and passing it to the Collections.sort() method.

package com.oraclejavacertified.java8.arraylist;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.List;

public class ArrayListReverseOrder1 {

    public static void main(String[] args) {

        // Creating ArrayList

        List<Integer> numbersList = new ArrayList<>();

        // Adding values to List

        numbersList.add(150);

        numbersList.add(50);

        numbersList.add(250);

        numbersList.add(500);

        numbersList.add(350);

        // printing before sorting

        System.out.println("Before sorting : " + numbersList);

        // Getting the descending order comparator

        Comparator<Integer> reverseComparator = Collections.reverseOrder();

        // Sorting with the reverse comparator with sort() method.

        // sort() method internally uses this comparator to sort in the descending order

        Collections.sort(numbersList, reverseComparator);

        // printing the final list after reverse order sorting. Original list only

        // sorted.

        System.out.println("After sorting : " + numbersList);

    }

}

Output:

Before sorting : [150, 50, 250, 500, 350]
After sorting : [500, 350, 250, 150, 50]

3.  Sort ArrayList In Descending order using Collections.reverse()


Next, look at the another way to sort the arraylist in descending order using two methods as below.

Collections.sort(arraylist); –> first sorts the list in the ascending order

Collections.reverse(arraylist); –> Next, reverse the sorted list.

Example:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
public class ArrayListReverseOrder2 {
 
    public static void main(String[] args) {
 
        // Creating ArrayList
        List<Integer> numbersList = new ArrayList<>();
 
        // Adding values to List
        numbersList.add(150);
        numbersList.add(50);
        numbersList.add(250);
        numbersList.add(500);
        numbersList.add(350);
 
        // printing before sorting
        System.out.println("Before sorting : " + numbersList);
 
        // sorting the list in the ascending order
        Collections.sort(numbersList);
 
        // reversing the sorted list into descending order
        Collections.reverse(numbersList);
 
        // printing the final list after reverse order sorting. Original list only
        // sorted.
        System.out.println("After sorting : " + numbersList);
 
    }
 
}

This program also produces the same output as in the section 2.

4. Java 8 Sort ArrayList Descending Order


Sorting list in reverse order is pretty easy from stream.sorted(Collections.reverseOrder()) in java 8 api.

We can use parallelStream() method to work efficiently with larger data volumes.

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
 
public class ArrayListReverseOrder2 {
 
    public static void main(String[] args) {
 
        // Creating ArrayList
        List<Integer> numbersList = new ArrayList<>();
 
        // Adding values to List
        numbersList.add(150);
        numbersList.add(50);
        numbersList.add(250);
        numbersList.add(500);
        numbersList.add(350);
 
        // printing before sorting
        System.out.println("Before sorting : " + numbersList);
 
        List<Integer> descendingList = numbersList.stream()
                .sorted(Collections.reverseOrder())
                .collect(Collectors.toList());
 
        // printing the final list after reverse order sorting. Original list only
        // sorted.
        System.out.println("After sorting : " + descendingList);
    }
}

Related Posts

0 comments:

Post a Comment