Natural Order Comparators by Guava’s Ordering class in Java
We have discussed natural order comparators provided by JDK in the previous post. This post will discuss natural order comparators provided by Guava Ordering class in Java.
The Ordering class is part of the Guava library and can be used to create and use comparators in Java. The Ordering class implements the Comparator interface, but also provides many additional methods to support common operations, such as sorting, filtering, finding minima and maxima, and more.
1. Using Ordering.natural() method
Guava library provides the static Ordering.natural() method that returns an Ordering object which uses the natural order of the values to compare them. This method is preferred over Comparator.naturalOrder() before Java 8. Here is an example of how we can use this method to sort an array in natural order:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
import com.google.common.collect.Ordering; import java.util.Arrays; class Main { public static void main(String[] args) { String[] s = { "B", "C", "A" }; Arrays.sort(s, Ordering.natural()); System.out.println(Arrays.toString(s)); // [A, B, C] } } |
The above code will throw a NullPointerException if the specified array contains any null value. The Guava Ordering class provides nullsFirst() method which returns an ordering that considers null to be less than non-null values. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import com.google.common.collect.Ordering; import java.util.Arrays; class Main { public static void main(String[] args) { String[] s = { "B", null, "C", "A", null }; // put all null values before non-null values Arrays.sort(s, Ordering.natural().nullsFirst()); System.out.println(Arrays.toString(s)); // [null, null, A, B, C] } } |
Note how we can chain different Ordering instances together to create comparators that can handle null values. We can also use the nullsLast() method, which returns an ordering that considers null to be greater than non-null values. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import com.google.common.collect.Ordering; import java.util.Arrays; class Main { public static void main(String[] args) { String[] s = { "B", null, "C", "A", null }; // put all null values after all non-null values Arrays.sort(s, Ordering.natural().nullsLast()); System.out.println(Arrays.toString(s)); // [A, B, C, null, null] } } |
2. Using Custom Ordering
We can also write our own custom natural order comparators by extending the Ordering class and overriding its compare() method. To illustrate, consider the following example which creates a custom natural order comparator using the Ordering class:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import com.google.common.collect.Ordering; import java.util.Arrays; class Main { public static void main(String[] args) { String[] s = { "B", "C", "A" }; // Guava Ordering class implements the Comparator interface Arrays.sort(s, new Ordering<String>() { @Override public int compare(String x, String y) { return x.compareTo(y); } }); System.out.println(Arrays.toString(s)); // [A, B, C] } } |
Like Ordering.natural(), the above implementation will throw a NullPointerException if the array contains any null value. To return an Ordering that considers null to be less than non-null values, we can modify the compare() method like below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import com.google.common.collect.Ordering; import java.util.Arrays; class Main { public static void main(String[] args) { String[] s = { "B", null, "C", "A", null }; Arrays.sort(s, new Ordering<>() { @Override public int compare(String x, String y) { if (x == null && y == null) { return 0; // return 0 if both `x` and `y` are null } if (x == null) { return -1; // return -1 if `x` is null and `y` is not null } if (y == null) { return 1; // return 1 if `y` is null and `x` is not null } return x.compareTo(y); // if both `x` and `y` are not null } }); System.out.println(Arrays.toString(s)); // [null, null, A, B, C] } } |
Similarly, to return an Ordering that considers null to be greater than non-null values, we can modify the compare() method like below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import com.google.common.collect.Ordering; import java.util.Arrays; class Main { public static void main(String[] args) { String[] s = { "B", null, "C", "A", null }; Arrays.sort(s, new Ordering<>() { @Override public int compare(String x, String y) { if (x == null && y == null) { return 0; // return 0 if both `x` and `y` are null } if (x == null) { return 1; // return 1 if `x` is null and `y` is not null } if (y == null) { return -1; // return -1 if `y` is null and `x` is not null } return x.compareTo(y); // if both `x` and `y` are not null } }); System.out.println(Arrays.toString(s)); // [A, B, C, null, null] } } |
That’s all about natural order comparators by Guava’s Ordering class.
Related Post:
Natural order comparators in Java by Apache Commons Collections
Reference: Ordering (Guava: Google Core Libraries for Java 31.0-jre API)
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)