Java Comparator Using Lambda
Learn how to use the power of lambdas to write a concise comparator.
We'll cover the following
If you’ve been working with Java for some time, then you’ve probably encountered a scenario where you need to sort the elements in a collection.
If your collection contains a wrapper class object then the sorting is very easy. Since all the wrapper classes implement the Comparable
interface, you can directly use Collections.sort()
to sort your collection.
However, if your collection contains a custom class object then you need to provide the logic to sort your object. In this lesson, we will look at an example in which we will sort a list of Person
class objects using a comparator. Then, we will write a program to do the same task using lambdas.
Get hands-on with 1200+ tech skills courses.