Open In App

Ordering Class | Guava | Java

A comparator, with additional methods to support common operations. This is an “enriched” version of Comparator. The common ways to get an instance of Ordering are :

Declaration : The declaration for com.google.common.collect.Ordering<T> class is :



@GwtCompatible
public abstract class Ordering<T>
   extends Object
      implements Comparator<T>

Below given are some methods provided by Guava’s Ordering Class :


Ordering() : This is a constructor of Ordering Class of Guava. It constructs a new instance of this class (only invokable by the subclass constructor, typically implicit). Some other methods provided by this Class are :

Exceptions :

Some other methods provided by this class are :


Example 1 :




// Java code to show implementation of
// Ordering class
import java.util.*;
  
import com.google.common.collect.Ordering;
  
class GFG {
  
    // Driver code
    public static void main(String args[])
    {
  
        // Creating a list of Integers
        List<Integer> myList = new ArrayList<Integer>();
  
        myList.add(new Integer(12));
        myList.add(new Integer(3));
        myList.add(new Integer(78));
        myList.add(new Integer(50));
        myList.add(new Integer(6));
        myList.add(new Integer(70));
        myList.add(new Integer(18));
        myList.add(new Integer(9));
        myList.add(new Integer(10));
  
        // Displaying natural order of numbers
        Ordering ordering = Ordering.natural();
        System.out.println("Input List : " + myList);
  
        // Displaying the sorted list
        Collections.sort(myList, ordering);
        System.out.println("Sorted List : " + myList);
    }
}

Output :



Input List : [12, 3, 78, 50, 6, 70, 18, 9, 10]
Sorted List : [3, 6, 9, 10, 12, 18, 50, 70, 78]

Below given are some other methods provided by Ordering Class of Guava :


Example 2 :




// Java code to show implementation of
// Ordering class
import java.util.*;
  
import com.google.common.collect.Ordering;
  
class GFG {
  
    // Driver code
    public static void main(String args[])
    {
  
        // Creating a list of Integers
        List<Integer> myList = new ArrayList<Integer>();
  
        myList.add(new Integer(12));
        myList.add(new Integer(3));
        myList.add(new Integer(78));
        myList.add(new Integer(50));
        myList.add(new Integer(6));
        myList.add(new Integer(70));
        myList.add(new Integer(18));
        myList.add(new Integer(9));
        myList.add(new Integer(10));
  
        // Displaying natural order of numbers
        Ordering ordering = Ordering.natural();
        System.out.println("Minimum element is : " + ordering.min(myList));
    }
}

Output :

Minimum element is : 3

Example 3 :




// Java code to show implementation of
// Ordering class
import java.util.*;
  
import com.google.common.collect.Ordering;
  
class GFG {
  
    // Driver code
    public static void main(String args[])
    {
  
        // Creating a list of Integers
        List<Integer> myList = new ArrayList<Integer>();
  
        myList.add(new Integer(12));
        myList.add(new Integer(3));
        myList.add(new Integer(78));
        myList.add(new Integer(50));
        myList.add(new Integer(6));
        myList.add(new Integer(70));
        myList.add(new Integer(18));
        myList.add(new Integer(9));
        myList.add(new Integer(10));
  
        // Displaying natural order of numbers
        Ordering ordering = Ordering.natural();
        System.out.println("Maximum element is : " + ordering.max(myList));
    }
}

Output :

Maximum element is : 78

Example 4 :




// Java code to show implementation of
// Ordering class
import java.util.*;
  
import com.google.common.collect.Ordering;
  
class GFG {
  
    // Driver code
    public static void main(String args[])
    {
  
        // Creating a list of Integers
        List<Integer> myList = new ArrayList<Integer>();
  
        myList.add(new Integer(12));
        myList.add(new Integer(3));
        myList.add(new Integer(78));
        myList.add(new Integer(50));
        myList.add(new Integer(6));
        myList.add(new Integer(70));
        myList.add(new Integer(18));
        myList.add(new Integer(9));
        myList.add(new Integer(10));
  
        // Displaying natural order of numbers
        Ordering ordering = Ordering.natural();
  
        // To get reverse of original list
        Collections.sort(myList, ordering.reverse());
  
        // Displaying the reversed elements
        System.out.println(myList);
    }
}

Output :

[78, 70, 50, 18, 12, 10, 9, 6, 3]

Reference : Google Guava


Article Tags :