The Comparator and Comparable implementations are available when a Collection needs to be sorted. In Java 8, you can also sort streams using the Comparator, and Employee is used as an example in this section
public class Employee { private Integer id; private String firstName; private String lastName; private Integer age; public Employee(Integer id, String firstName, String lastName, Integer age){ this.id = id; this.firstName = firstName; this.lastName = lastName; this.age = age; } //Other getter and setter methods @Override public String toString() { return "\n["+this.id+","+this.firstName+","+this.lastName+","+this.age+"]"; }}Copy the code
Gets a list of Employees
private static List<Employee> getEmployees(){
List<Employee> employees = new ArrayList<>();
employees.add(new Employee(1,"Lokesh", "Gupta", 32));
employees.add(new Employee(2,"Aman", "Sharma", 28));
employees.add(new Employee(3,"Aakash", "Yaadav", 52));
employees.add(new Employee(4,"James", "Hedge", 72));
employees.add(new Employee(5,"David", "Kameron", 19));
employees.add(new Employee(6,"Yash", "Chopra", 25));
employees.add(new Employee(7,"Karan", "Johar", 59));
employees.add(new Employee(8,"Balaji", "Subbu", 88));
employees.add(new Employee(9,"Vishu", "Bissi", 33));
employees.add(new Employee(10,"Lokesh", "Ramachandran", 60));
return employees;
}Copy the code
Sort by firstName
List<Employee> employees = getEmployees();
//Sort all employees by first name
employees.sort(Comparator.comparing(e -> e.getFirstName()));
//OR you can use below
employees.sort(Comparator.comparing(Employee::getFirstName));
//Let's print the sorted list
System.out.println(employees);Copy the code
The output
[[3,Aakash,Yaadav,52],
[2,Aman,Sharma,28],
[8,Balaji,Subbu,88],
[5,David,Kameron,19],
[4,James,Hedge,72],
[7,Karan,Johar,59],
[1,Lokesh,Gupta,32],
[10,Lokesh,Ramachandran,60],
[9,Vishu,Bissi,33],
[6,Yash,Chopra,25]]Copy the code
Sorting inversion
A sort can be reversed using comparator.reversed ()
List<Employee> employees = getEmployees();
//Sort all employees by first name; And then reversed
Comparator<Employee> comparator = Comparator.comparing(e -> e.getFirstName());
employees.sort(comparator.reversed());
//Let's print the sorted list
System.out.println(employees);Copy the code
The output
[[6,Yash,Chopra,25],
[9,Vishu,Bissi,33],
[1,Lokesh,Gupta,32],
[10,Lokesh,Ramachandran,60],
[7,Karan,Johar,59],
[4,James,Hedge,72],
[5,David,Kameron,19],
[8,Balaji,Subbu,88],
[2,Aman,Sharma,28],
[3,Aakash,Yaadav,52]]Copy the code
Sort by lastName
List<Employee> employees = getEmployees();
//Sort all employees by first name
employees.sort(Comparator.comparing(e -> e.getLastName()));
//OR you can use below
employees.sort(Comparator.comparing(Employee::getLastName));
//Let's print the sorted list
System.out.println(employees);Copy the code
The output
[[9,Vishu,Bissi,33],
[6,Yash,Chopra,25],
[1,Lokesh,Gupta,32],
[4,James,Hedge,72],
[7,Karan,Johar,59],
[5,David,Kameron,19],
[10,Lokesh,Ramachandran,60],
[2,Aman,Sharma,28],
[8,Balaji,Subbu,88],
[3,Aakash,Yaadav,52]]Copy the code
Sort by firstName, then lastName
Use the thenComparing ()
List<Employee> employees = getEmployees();
//Sorting on multiple fields; Group by.
Comparator<Employee> groupByComparator = Comparator.comparing(Employee::getFirstName)
.thenComparing(Employee::getLastName);
employees.sort(groupByComparator);
System.out.println(employees);Copy the code
Parallel sorting
ParallelSort () is all you need to do for parallel sorting.
//Parallel Sorting
Employee[] employeesArray = employees.toArray(new Employee[employees.size()]);
//Parallel sorting
Arrays.parallelSort(employeesArray, groupByComparator);
System.out.println(employeesArray);Copy the code
The output
[3,Aakash,Yaadav,52],
[2,Aman,Sharma,28],
[8,Balaji,Subbu,88],
[5,David,Kameron,19],
[4,James,Hedge,72],
[7,Karan,Johar,59],
[1,Lokesh,Gupta,32], //These both employees are
[10,Lokesh,Ramachandran,60], //sorted on last name as well
[9,Vishu,Bissi,33],
[6,Yash,Chopra,25]Copy the code
Use count() and counting() to count quantities
Count () is a terminal operation that counts streams and returns a long. Counting () is a Collector method.
Use the count ()
public static void main(String[] args) { long count = Stream.of("how","to","do","in","java").count(); System.out.printf("There are %d elements in the stream %n", count); Count = IntStream. Of,2,3,4,5,6,7,8,9 (1). The count (); System.out.printf("There are %d elements in the stream %n", count); ,2,3,4,5,6,7,8,9 count = LongStream. Of (1) the filter (I - > I % 2 = = 0). The count (); System.out.printf("There are %d elements in the stream %n", count); }Copy the code
Use the counting ()
public static void main(String[] args) { long count = Stream.of("how","to","do","in","java").collect(Collectors.counting()); System.out.printf("There are %d elements in the stream %n", count); Count = Stream of (1,2,3,4,5,6,7,8,9). Collect (Collectors. Counting ()); System.out.printf("There are %d elements in the stream %n", count); Filter (I -> I %2 == 0). Collect (Collectors. Counting ()); count = stream.of (1,2,3,4,5,6,7,8,9). System.out.printf("There are %d elements in the stream %n", count); }Copy the code