Java8 lambda comparator Realtime Examples
In this tutorial, we will learn about using comparators with Lamda. Comparators are extensively used in collections to sort objects link users, employees, orders etc.
In Java 8, comparators can be created using lambda expressions or method references.
Example of how to use a lambda expression to create a comparator:
List<UserInfo> usersData = Arrays.asList(
new UserInfo("Jayaram", 25),
new UserInfo("javasavvu", 30),
new UserInfo("test", 20)
);
Collections.sort(usersData, (u1, u2) -> u1.getAge() - u2.getAge());
In the above examples, (u1, u2) -> u1.getAge() - u2.getAge()
is used to compare two UserInfo
objects based on their age. The Collections.sort
method sorts the list of users based on the comparison result provided by the lambda expression.
Here’s another example using a method reference:
List<UserInfo> usersData = Arrays.asList(
new UserInfo("Jayaram", 25),
new UserInfo("javasavvu", 30),
new UserInfo("test", 20)
);
usersData.sort(Comparator.comparing(UserInfo::getAge));
In this example, the method reference UserInfo::getAge
is used to create a Comparator
that compares two UserInfo
objects based on their age. The Comparator.comparing
method takes a Function
that extracts a Comparable
from the objects being compared, and the method reference is used to create a Comparator
that sorts the list of people based on their age.
java 8 comparator on multiple fields
Complex sorting using lambda expressions in Java 8 is possible using the Comparator.comparing
and Comparator.thenComparing
methods. The Comparator.comparing
method takes a Function
that extracts a Comparable
from the objects being compared, and the Comparator.thenComparing
method can be used to specify additional sorting criteria in case of ties.
Here’s an example of complex sorting using lambda expressions:
List<Person> people = Arrays.asList(
new Person("Jayaram", 25, "New York"),
new Person("Jay", 30, "London"),
new Person("Ram", 20, "Paris"),
new Person("Vinay", 25, "Berlin")
);
people.sort(
Comparator.comparing(Person::getAge)
.thenComparing(Person::getName)
.thenComparing(Person::getCity)
);
In this example, the objects in the people
list are sorted first by age, then by name, and finally by city. The Comparator.comparing
method is used to extract the values to compare, and the Comparator.thenComparing
method is used to specify additional sorting criteria.
Java8 String Comparator Examples
List<String> fruits = Arrays.asList("Apple", "Mango", "BlueBerries" "Banana", "Orange", "Papaya");
// Sorted in natural order
List<String> sorted = fruits.stream().sorted(Comparator.naturalOrder()).collect(Collectors.toList());
// Sorted in the reverse of the natural order
sorted = fruits.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
// Sorted by length
sorted = bonds.stream().sorted(Comparator.comparingInt(String::length)).collect(Collectors.toList());
// Sorted lowercase
sorted = bonds.stream().sorted(Comparator.comparing(String::toLowerCase)).collect(Collectors.toList());
// Sorting by Multiple Fields
sorted = bonds.stream().sorted(Comparator.comparingInt(String::length).thenComparing(Comparator.naturalOrder())).collect(Collectors.toList());