![]() ![]() But how can Collections.reverseOrder() return this no matter what T is? For example, it is not possible for something to be Comparator and Comparator at the same time. In the implementation of the library you are looking at, it has a single global comparator instance, ReverseComparator.REVERSE_ORDER. As for how the Collections.reverseOrder() gets a value of the type Comparator, that's an internal implementation detail of the library. It might, for example, infer T = Integer, so the Collections.reverseOrder() call will return a Comparator (or it is also valid to infer T = Object, so that Collections.reverseOrder() will return a Comparator it doesn't matter either way). The compiler sees that you want the Collections.reverseOrder() method to return the type Comparator, so it infers the T in the call to Collections.reverseOrder() that will make it return a Comparator. I recommended checking out the other posts on Comparator.The variable comparator has type Comparator (or Comparator it doesn't matter for our purposes). In this post, we have learnt about the Comparator methods naturalOrder, reverseOrder and reversed. Note this is the reverse of the previous sorted orders output (i.e., when using sortByTotal). Prints,, OrderId: 1, Items:, OrderId: 2, Items: ] SortedOrders.sort(sortByTotal.reversed()) List sortedOrders = new ArrayList(orders) ![]() If we want to sort the other way around (the highest total to lowest), we can call the reversed() method on the sortByTotal comparator as shown below. In other words, using the sortByTotal comparator, we have sorted the orders list from lowest to highest in terms of the order amount (total). Using sortByTotal comparator to sort the list of orders, we get the order with the lowest total as the first item of the list. Prints,, OrderId: 1, Items:, OrderId: 3, Items: ] List orders = List.of( //seeĬomparator sortByTotal = paringDouble(Order::getTotal) Let us use the same Order list and we have a comparator that orders by the total of the Order instance (rather than by the order id). But rather than taking a comparator as a parameter, it returns a reversed comparator of the comparator instance on which this method is called. Similar to the reverseOrder() method, it returns a comparator that imposes a reverse ordering. Thus, we can call it only on a Comparator instance. Unlike the naturalOrder and reverseOrder methods, this is not a static method. The final method we will see is the reversed method. Since we are using the reverse of the natural order, we get the order objects in the order – 3, 2 and then 1. ![]() Prints,, OrderId: 2, Items:, OrderId: 1, Items: ] SortedOrders.sort(Comparator.reverseOrder()) Using it in our orders example, List orders = List.of( It returns a comparator that imposes the reverse of the natural order. public class Order implements Comparable ![]() It has the order id, the total, the customer id for which the order belongs to and the list of items in the order. Example setupįor this post, let us use a simple Order class shown below. As a follow up of that, in this post we will learn about the Comparator naturalOrder, reverseOrder and reversed methods. I have written a post on the Comparator comparing methods earlier. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |