Natural Order Comparators by Apache Commons Collections in Java
We have already discussed natural order comparators provided by JDK. This post will discuss natural order comparators provided by Apache Commons Collections library in Java.
The natural order comparator is useful when we want to sort a collection of objects that have a natural ordering, such as numbers, strings, dates, etc. The natural order is defined by the Comparable interface, which means that the objects to be compared must implement this interface. We can use the Apache Commons Collections to implement the natural comparators in Java. Some of the methods are:
1. Using naturalComparator() method
Apache Commons Collections ComparatorUtils class provides static naturalComparator() method that returns a comparator that uses the natural order of the objects. For example, we can use the natural order comparator to sort an array of strings as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import org.apache.commons.collections4.ComparatorUtils; import java.util.Arrays; import java.util.Comparator; class Main { public static void main(String[] args) { String[] s = { "B", "C", "A" }; // get the natural order comparator Comparator<String> naturalComparator = ComparatorUtils.naturalComparator(); // sort the list using the natural order comparator Arrays.sort(s, naturalComparator); System.out.println(Arrays.toString(s)); // [A, B, C] } } |
We can also use ComparatorUtils.NATURAL_COMPARATOR that provides a comparator for natural sort order of the objects. It is equivalent to ComparatorUtils.naturalComparator().
|
1 2 3 4 5 6 7 8 9 10 11 12 |
import org.apache.commons.collections4.ComparatorUtils; import java.util.Arrays; class Main { public static void main(String[] args) { String[] s = { "B", "C", "A" }; Arrays.sort(s, ComparatorUtils.NATURAL_COMPARATOR); System.out.println(Arrays.toString(s)); // [A, B, C] } } |
2. Using nullLowComparator() and nullHighComparator() method
If the array contains any null value, ComparatorUtils.naturalComparator() will throw a NullPointerException. The ComparatorUtils class provides static nullLowComparator() method to handle nulls. It returns a comparator that considers a null value to be less than any non-null value and equal to any other null value. The natural order of the objects is used when both of them are not null. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import org.apache.commons.collections4.ComparatorUtils; import java.util.Arrays; import java.util.Comparator; class Main { public static void main(String[] args) { String[] s = { "B", null, "C", "A", null }; // get the null low comparator Comparator<String> naturalComparator = ComparatorUtils.nullLowComparator(null); // sort the list using the null low comparator Arrays.sort(s, naturalComparator); System.out.println(Arrays.toString(s)); // [null, null, A, B, C] } } |
The ComparatorUtils class also provides static nullHighComparator() method to handle nulls. It returns a comparator that considers a null value to be greater than any non-null value and equal to any other null value. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import org.apache.commons.collections4.ComparatorUtils; import java.util.Arrays; import java.util.Comparator; class Main { public static void main(String[] args) { String[] s = { "B", null, "C", "A", null }; // get the null high comparator Comparator<String> nullHighComparator = ComparatorUtils.nullHighComparator(null); // sort the list using the null high comparator Arrays.sort(s, nullHighComparator); System.out.println(Arrays.toString(s)); // [A, B, C, null, null] } } |
3. Using NullComparator() constructor
Apache Commons Collections also provides NullComparator class whose instance will place null higher or lower than any non-null object it is compared with. We can create a NullComparator using the NullComparator() constructor that takes a boolean argument. This argument determines whether null values are higher or lower than non-null values. If it is true, null values are higher; if it is false, they are lower. The natural order of the objects is used when both of them are not null. For example:
|
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 org.apache.commons.collections4.comparators.NullComparator; import java.util.Arrays; import java.util.Comparator; class Main { public static void main(String[] args) { String[] s = { "B", null, "C", "A", null }; // create a null comparator that sorts null lower than any non-null object Comparator<String> nullLowComparator = new NullComparator<>(false); // sort the list using the null comparator Arrays.sort(s, nullLowComparator); System.out.println(Arrays.toString(s)); // [null, null, A, B, C] // create a null comparator that sorts null higher than any non-null object Comparator<String> nullHighComparator = new NullComparator<>(true); // sort the list using the null comparator Arrays.sort(s, nullHighComparator); System.out.println(Arrays.toString(s)); // [A, B, C, null, null] } } |
That’s all about natural order comparators in Apache Commons Collections in Java.
Related Post:
References:
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 :)