Guava ordering is a powerhouse class that simplifies using java comparator interface. In part one we will show basics on how call reverse, find the min and max, create Ordering object from existing comparator and position nulls within a list.
Detailed Video Notes
Hey everyone, this is Justin from level up lunch in episode 7 we talked about how java 8 has simplified sorting and has removed complexities while using lambda expressions. If you have adopted java 8, which I imagine many of you haven't, guava provides this powerhouse class called Ordering. Ordering is a fluent comparator that allows you to build complex ways to order collections. In this episode we will show a series of basic examples that use the Ordering class. If you haven't used FluentIterable, Guava's FluentIterable provides a rich interface for manipulating Iterables while Ordering class "enriched" version of Comparator.
A few things to note about the Ordering. The Ordering class implements comparator which allows backwards compatibility with Collections.sort or other classes built that expect a comparator. This will still allow you to take advantage of features such as chaining, compounding multiple comparators, and creating an Ordering class from an existing comparator you might have. So if you are an architect that gets a little excited about introducing new classes or libraries, guava's Ordering should meet your check mark.
For our first series of examples we will create a list of numbers and populate the list with random integers. The first snippet we will show how to order an arraylist in natural order of elements. We will create an Ordering object by calling the Ordering.natural() which will order numbers by least to greatest or lowest to highest.
By calling Ordering.natural().reversed() we can create a comparator in reverse order which will sort the arraylist from highest to lowest or greatest to least values. Chaining these operations together allows a very natural way to pass comparators to Collections.sort.
Next is min max so if you want to find the maximum value in a collection or minimum value in a collection you can do so by Ordering.natural().max() which should return the greatest value in a collection. Same thing with the minimum by calling Ordering.natural().min() which should return the least value in the collection.
Create Ordering from Comparator
The next snippet will create an Ordering object based on an existing Comparator. We have created a comparator that will sort Strings by length and a list of strings. Calling Ordering.from and passing in the byLength we will create an Ordering instance based on the comparator. The Ordering class is smart enough to start from an existing comparator which allows you to consume it. Below we call .reversed() or chain another operation which will allows us to sort a list of strings with the longest value first.
Check if a collection is ordered
A neat feature is the Ordering class has the ability to check if a collection is ordered based on a comparator by calling the Ordering.isOrdered. So if you are writing a lot of comparators and you want to write a unit test, this gives you a easy way to validate comparators are working.
Using Ordering chaining mechanism, we can call nullsFirst which will order nulls before non-null elements or nullLast which will order null after all non-null values.
Ordering.leastOf will return the a specified number of elements in the beginning of the given iterable according to this ordering. This is is similar taking a limit of a collection or taking a number of elements in an arrayList. Ordering.greatestOf is just the opposite where it will take N number of elements from the end of the collection.
While we we just scratched the surface of guava's Ordering we hope this screencast provided you with a high level look at the features it provides. Thanks for joining in today's level up lunch.