Java 8 Sorting with Lambdas
Learn the basics of sorting with Java 8 lambdas. Here's a writeup with more practical examples: http://www.baeldung.com/java-8-sort-lambda =============================== 1. Overview ========= In this tutorial, we're going to take a first look at the Lambda support in Java 8 - specifically at how to leverage it to write the Comparator and sort a Collection. This article is part of the “Java – Back to Basic” series here on Baeldung. First, let's define a simple entity class: public class Human { ... } 2. Basic Sort without Lambdas ========================= Before Java 8, sorting a collection would involve creating an anonymous inner class for the Comparator used in the sort. This would simply be used to sort the List of Human entities. 3. Basic Sort with Lambda Support ============================ With the introduction of Lambdas, we can now bypass the anonymous inner class and go achieve the same result with simple, functional semantics. Similarly - we can now test the behavior just as before. Notice that we're also using the new sort API added to java.util.List in Java 8 - instead of the old Collections.sort API. 4. Basic Sorting with no Type Definitions ================================ We can further simplify the expression by not specifying the type definitions - the compiler is capable of inferring these on its own. And again, the test remains very similar. 5. Sort using Reference to Static Method ================================ Next, we're going to perform the sort using a Lambda Expression with a reference to a static method. First, we're going to define the method compareByNameThenAge - with the exact same signature as the compare method in a Comparator object. Now, we're going to call the humans.sort method with this reference: humans.sort(Human::compareByNameThenAge); The end result is a working sorting of the collection using the static method as a Comparator. 6. Sort Extracted Comparators ========================= We can also avoid defining even the comparison logic itself by using an instance method reference and the Comparator.comparing method - which extracts and creates a Comparable based on that function. We're going to use the getter getName() to build the Lambda expression and sort the list by name. 7. Reverse Sort ============ JDK 8 has also introduced a helper method for reversing the comparator - we can make quick use of that to reverse our sort. 8. Sort with Multiple Conditions ========================= The comparison lambda expressions need not be this simple - we can write more complex expressions as well - for example sorting the entities first by name, and then by age. 9. Sort with Multiple Conditions - Composition ===================================== The same comparison logic - first sorting by name and then, secondarily, by age - can also be implemented by the new composition support for Comparator. Starting with JDK 8, we can now chain together multiple comparators to build more complex comparison logic. =============================== The instructor in this lesson is Thomas Hehl
Download
0 formatsNo download links available.