The Wayback Machine - https://web.archive.org/web/20240710084551/https://www.geeksforgeeks.org/ordering-class-guava-java/
Open In App

Ordering Class | Guava | Java

Last Updated : 14 Jun, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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 :

  • Subclass it and implement compare(T, T) instead of implementing Comparator directly.
  • Pass a pre-existing Comparator instance to from(Comparator).
  • Use the natural ordering, natural().

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 :

  • explicit(List valuesInOrder) : NullPointerException if any of the provided values is null, IllegalArgumentException if valuesInOrder contains any duplicate values.
  • explicit(T leastValue, T… remainingValuesInOrder) : NullPointerException if any of the provided values is null, IllegalArgumentException if any duplicate values.
  • min(Iterator iterator) : NoSuchElementException if iterator is empty, ClassCastException if the parameters are not mutually comparable under this ordering.
  • min(Iterable iterable) : NoSuchElementException if iterable is empty, ClassCastException if the parameters are not mutually comparable under this ordering.
  • min(E a, E b) : ClassCastException if the parameters are not mutually comparable under this ordering.
  • min(E a, E b, E c, E… rest) : ClassCastException if the parameters are not mutually comparable under this ordering.
  • max(Iterator iterator) : NoSuchElementException if iterator is empty, ClassCastException if the parameters are not mutually comparable under this ordering.
  • max(Iterable iterable) : NoSuchElementException if iterable is empty, ClassCastException if the parameters are not mutually comparable under this ordering.
  • max(E a, E b) : ClassCastException if the parameters are not mutually comparable under this ordering.
  • max(E a, E b, E c, E… rest) : ClassCastException if the parameters are not mutually comparable under this ordering.
  • leastOf(Iterable iterable, int k): IllegalArgumentException if k is negative.
  • leastOf(Iterator elements, int k) : IllegalArgumentException if k is negative.
  • greatestOf(Iterable iterable, int k): IllegalArgumentException if k is negative.
  • greatestOf(Iterator elements, int k) : IllegalArgumentException if k is negative.
  • immutableSortedCopy : NullPointerException if any of elements (or elements itself) is null.

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



Similar Reads

Range Class | Guava | Java
Guava’s Range represents an interval, for example, a &lt; range &lt; b. Here range includes any value between a and b, called endpoints which form the boundary. Any value between the boundary is a contiguous span of values of type Comparable. Declaration : The declaration for com.google.common.collect.Range&lt;C&gt; class is : @GwtCompatible public
5 min read
BigIntegerMath Class | Guava | Java
BigIntegerMath is used to perform mathematical operations on BigInteger values. Basic standalone math functions are divided into the classes IntMath, LongMath, DoubleMath, and BigIntegerMath based on the primary numeric type involved. These classes have parallel structure, but each supports only the relevant subset of functions. Similar functionali
3 min read
IntMath Class | Guava | Java
Introduction : IntMath is used to perform mathematical operations on Integer values. Basic standalone math functions are divided into the classes IntMath, LongMath, DoubleMath, and BigIntegerMath based on the primary numeric type involved. These classes have parallel structure, but each supports only the relevant subset of functions. Declaration :
3 min read
LongMath Class | Guava | Java
LongMath is used to perform mathematical operations on Long values. Basic standalone math functions are divided into the classes IntMath, LongMath, DoubleMath, and BigIntegerMath based on the primary numeric type involved. These classes have parallel structure, but each supports only the relevant subset of functions. Declaration : The declaration f
3 min read
Ints Class | Guava | Java
Ints is a utility class for primitive type int. It provides Static utility methods pertaining to int primitives, that are not already found in either Integer or Arrays. Declaration : @GwtCompatible(emulated=true) public final class Ints extends Object Below table shows the Field summary for Guava Ints Class : Some of the methods provided by Ints Cl
3 min read
Chars Class | Guava | Java
Chars is a utility class for primitive type char. It provides Static utility methods pertaining to char primitives, that are not already found in either Character or Arrays. All the operations in this class treat char values strictly numerically, i.e, they are neither Unicode-aware nor locale-dependent. Declaration : @GwtCompatible(emulated=true) p
3 min read
Longs Class | Guava | Java
Longs is a utility class for primitive type long. It provides Static utility methods pertaining to long primitives, that are not already found in either Long or Arrays. Declaration : @GwtCompatible(emulated=true) public final class Longs extends Object Below table shows the Field summary for Guava Longs Class : Some of the methods provided by Guava
3 min read
Bytes Class | Guava | Java
Bytes is a utility class for primitive type byte. It provides Static utility methods pertaining to byte primitives, that are not already found in either Byte or Arrays and interpret bytes as neither signed nor unsigned. The methods which specifically treat bytes as signed or unsigned are found in SignedBytes and UnsignedBytes. Declaration : @GwtCom
3 min read
Shorts Class | Guava | Java
Shorts is a utility class for primitive type short. It provides Static utility methods pertaining to short primitives, that are not already found in either Short or Arrays. Declaration : @GwtCompatible(emulated=true) public final class Shorts extends Object Below table shows the Field summary for Guava Shorts Class : Some of the methods provided by
3 min read
Doubles Class | Guava | Java
Doubles is a utility class for primitive type double. It provides Static utility methods pertaining to double primitives, that are not already found in either Double or Arrays. Declaration : @GwtCompatible(emulated=true) public final class Doubles extends Object Below table shows the Field summary for Guava Doubles Class : Some of the methods provi
3 min read
Article Tags :
Practice Tags :