Java 8 Lambda: Comparator Example
In this article, we will show you how to work with Java 8 Lambda expressions using the Comparator interface in Java.
1. Sort Without Lambda
Before Java 8 was released, we had to create an anonymous inner class for the Comparator to sort a collection.
1 2 3 4 5 6 |
new Comparator<User>() { @Override public int compare(User o1, User o2) { return o1.getAge() - o2.getAge(); } } |
Then we had to transfer the inner class and the collection to Collections.sort
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
public static void main(String[] args) { List<User> users = Arrays.asList( new User("John", 28), new User("Jane", 35), new User("Alex", 21)); System.out.println("Before sort:"); for (User user : users) { System.out.println(user); } Collections.sort(users, new Comparator<User>() { @Override public int compare(User o1, User o2) { return o1.getAge() - o2.getAge(); } }); System.out.println("\nAfter sort:"); for (User user : users) { System.out.println(user); } } |
Output:
1 2 3 4 5 6 7 8 9 |
Before sort: User{name='John', age=28} User{name='Jane', age=35} User{name='Alex', age=21} After sort: User{name='Alex', age=21} User{name='John', age=28} User{name='Jane', age=35} |
2. Sort With Lambda
In Java 8, we use the Lambda expression instead of creating an anonymous inner class for the Comparator.
1 |
(User o1, User o2) -> o1.getAge() - o2.getAge() |
When we use the Lambda expression, the code is shorter.
By the way, you can shorten the code even more by leaving out the types for 01 and 02. If we don't include them, the code will look like this:
1 |
(o1, o2) -> o1.getAge() - o2.getAge() |
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public static void main(String[] args) { List<User> users = Arrays.asList( new User("John", 28), new User("Jane", 35), new User("Alex", 21)); System.out.println("Before sort:"); for (User user : users) { System.out.println(user); } Collections.sort(users, (o1, o2) -> o1.getAge() - o2.getAge()); System.out.println("\nAfter sort:"); for (User user : users) { System.out.println(user); } |
Output:
1 2 3 4 5 6 7 8 9 |
Before sort: User{name='John', age=28} User{name='Jane', age=35} User{name='Alex', age=21} After sort: User{name='Alex', age=21} User{name='John', age=28} User{name='Jane', age=35} |
3. List.sort()
For Java 8, the sort() method was added to the List Interface. And as a result, instead of writing code like this:
1 |
Collections.sort(users, (o1, o2) -> o1.getAge() - o2.getAge()); |
we can now write code like this:
1 |
users.sort((o1, o2) -> o1.getAge() - o2.getAge()); |
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public static void main(String[] args) { List<User> users = Arrays.asList( new User("John", 28), new User("Jane", 35), new User("Alex", 21)); System.out.println("Before sort:"); for (User user : users) { System.out.println(user); } users.sort((o1, o2) -> o1.getAge() - o2.getAge()); System.out.println("\nAfter sort:"); for (User user : users) { System.out.println(user); } } |
Output:
1 2 3 4 5 6 7 8 9 |
Before sort: User{name='John', age=28} User{name='Jane', age=35} User{name='Alex', age=21} After sort: User{name='Alex', age=21} User{name='John', age=28} User{name='Jane', age=35} |
4. Reversed Sorting
With Java 8, reversed sorting has become much easier. You no longer have to make changes to already created Comparators.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public static void main(String[] args) { List<User> users = Arrays.asList( new User("John", 28), new User("Jane", 35), new User("Alex", 21)); System.out.println("Before sort:"); for (User user : users) { System.out.println(user); } Comparator<User> comparator = (o1, o2) -> o1.getAge() - o2.getAge(); users.sort(comparator.reversed()); System.out.println("\nAfter sort:"); for (User user : users) { System.out.println(user); } } |
Output:
1 2 3 4 5 6 7 8 9 |
Before sort: User{name='John', age=28} User{name='Jane', age=35} User{name='Alex', age=21} After sort: User{name='Jane', age=35} User{name='John', age=28} User{name='Alex', age=21} |
5. More Lambda Examples
5.1 Sort By Name
Before Java 8:
1 2 3 4 5 6 |
Collections.sort(users, new Comparator<User>() { @Override public int compare(User o1, User o2) { return o1.getName().compareTo(o2.getName()); } }); |
Now:
1 |
users.sort((o1, o2) -> o1.getName().compareTo(o2.getName())); |
Output:
1 2 3 |
User{name='Alex', age=21} User{name='Jane', age=35} User{name='John', age=28} |
5.2 Reversed Sort By Name
Before Java 8:
1 2 3 4 5 6 |
Collections.sort(users, new Comparator<User>() { @Override public int compare(User o1, User o2) { return o2.getName().compareTo(o1.getName()); } }); |
Now:
1 2 |
Comparator<User> c = (o1, o2) -> o1.getName().compareTo(o2.getName()); users.sort(c.reversed()); |
Note that the Comparator didn't change.
Output:
1 2 3 |
User{name='John', age=28} User{name='Jane', age=35} User{name='Alex', age=21} |
5.3 Sort By Using a Few Conditions
Before Java 8:
1 2 3 4 5 6 7 8 |
Collections.sort(users, new Comparator<User>() { @Override public int compare(User o1, User o2) { if (o1.getAge() == o2.getAge()) return o1.getName().compareTo(o2.getName()); else return o1.getAge() - o2.getAge(); } }); |
Now:
1 2 3 4 5 |
users.sort((o1, o2) -> { if (o1.getAge() == o2.getAge()) return o1.getName().compareTo(o2.getName()); else return o1.getAge() - o2.getAge(); }); |
Output:
1 2 3 |
User{name='John', age=28} User{name='Jane', age=35} User{name='Alex', age=21} |