In this tutorials, we are going to see how to sort user defined objects using Java 8 comparator with lambda expression. In the previous tutorials we have discussed about what is comparator in Java, you can seen the basic functionality of the comparator in java here.
Java 8 Comparator :
Sorting the Cricketer objects with comparator with Java 8 extreme feature Lambda expressions.
Create a Cricketer Pojo class.
Cricketer.java
Cricketer
class Cricketer {
private int id;
private String name;
private int score;
public Cricketer(int id, String name, int score) {
super();
this.id = id;
this.name = name;
this.score = score;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getScore() {
return score;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", score=" + score
+ "]";
}
}
Comparator with Lambda :
Cricketer.java
package com.onlinetutorialspoint.java8;
import java.util.Arrays;
import java.util.List;
public class Java8_Comparator_with_Lambda {
public static void main(String[] args) {
List<Cricketer> cricketer = Arrays.asList(
new Cricketer(1, "Dhoni", 102), new Cricketer(2, "Virat Kohli",
98), new Cricketer(3, "Rohit Sharma", 75),
new Cricketer(4, "Youraj Singh", 84), new Cricketer(5, "Raina",
98));
System.out.println("Before Sorting..");
cricketer.forEach(System.out::println);
sortByName(cricketer);
}
public static void sortByScore(List<Cricketer> crickters) {
crickters.sort((Cricketer o1, Cricketer o2) -> o1.getScore()
- o2.getScore());
System.out.println("After Sorting..");
crickters.forEach((developer) -> System.out.println(developer));
}
public static void sortByName(List<Cricketer> crickters) {
crickters.sort((Cricketer c1, Cricketer c2) -> c1.getName().compareTo(
c2.getName()));
System.out.println("After Sorting..");
crickters.forEach((developer) -> System.out.println(developer));
}
}
Output:
Terminal
Before Sorting..
Student [id=1, name=Dhoni, score=102]
Student [id=2, name=Virat Kohli, score=98]
Student [id=3, name=Rohit Sharma, score=75]
Student [id=4, name=Youraj Singh, score=84]
Student [id=5, name=Raina, score=98]
After Sorting..
Student [id=1, name=Dhoni, score=102]
Student [id=5, name=Raina, score=98]
Student [id=3, name=Rohit Sharma, score=75]
Student [id=2, name=Virat Kohli, score=98]
Student [id=4, name=Youraj Singh, score=84]
Comparing with Stream :
Java8_Comparator_with_Lambda.java
package com.onlinetutorialspoint.java8;
import java.util.Arrays;
import java.util.Comparator;
import static java.util.Comparator.*;
import java.util.List;
public class Java8_Comparator_with_Lambda {
public static void main(String[] args) {
List<Cricketer> cricketer = Arrays.asList(
new Cricketer(1, "Dhoni", 102), new Cricketer(2, "Virat Kohli",
98), new Cricketer(3, "Rohit Sharma", 75),
new Cricketer(4, "Youraj Singh", 84), new Cricketer(5, "Raina",
98));
sortUsingStream(cricketer, comparing(Cricketer::getName));
}
public static void sortUsingStream(List<Cricketer> crickters,Comparator<Cricketer> comp){
crickters.stream().sorted(comp).forEach(System.out::println);
}
}
Here comparing() is a static method from java.util.Comparator, it accepts a function that extracts a java.lang.Comparable sort key from a type {T}, and returns a Comparator<T> that compares by that sort key.
Output:
Terminal
Student [id=1, name=Dhoni, score=102]
Student [id=5, name=Raina, score=98]
Student [id=3, name=Rohit Sharma, score=75]
Student [id=2, name=Virat Kohli, score=98]
Student [id=4, name=Youraj Singh, score=84]
Happy Learning:)