Java 8 Comparator with lambda

This commit is contained in:
Umesh Awasthi
2021-03-22 21:47:06 -07:00
parent b4a08a35e8
commit f2d950c455
3 changed files with 123 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.javadevjournal</groupId>
<artifactId>comparator-lambda-expression</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>15</maven.compiler.source>
<maven.compiler.target>15</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,84 @@
package com.javadevjournal;
import com.javadevjournal.data.Customer;
import java.util.*;
public class CustomerService {
public static void main(String[] args) {
sortByAge();
sortByFirstAndSecondName();
sortInReverseOrder();
}
private static void sortInReverseOrder(){
List<Customer> customerList = getCustomers();
Comparator<Customer> customerComparator = Comparator.comparing(e-> e.getAge());
customerList.sort(customerComparator.reversed());
customerList.forEach(System.out::println);
}
private static void sortByFirstAndSecondName(){
List<Customer> customers = getCustomers();
//Let's get the comparator
Comparator<Customer> comparator = Comparator.comparing(Customer::getFirstName)
.thenComparing(Customer::getLastName);
customers.sort(comparator);
//output
customers.forEach(System.out::println);
}
private static void sortByAge(){
List<Customer> customers = getCustomers();
customers.sort(Comparator.comparing(Customer::getAge));
//customers.forEach(System.out::println);
//option 2
List<Customer> customers1 = getCustomers();
customers1.sort(Comparator.comparing(e->e.getAge()));
customers1.forEach(System.out::println);
//option 3 if we know the type
customers.sort(Comparator.comparingInt(Customer::getAge));
//pure lambda expression
Collections.sort(customers, (c1, c2) -> c1.getAge() - c1.getAge()); //we careful when using this as it can cause overflow
}
private static List<Customer> getCustomers(){
List<Customer> customers = new ArrayList();
customers.add(Customer.builder()
.firstName("Robert").lastName("Hickle")
.age(62).email("abc@demo1.com")
.build());
customers.add(Customer.builder()
.firstName("Sandra").lastName("Hadleigh")
.age(21).email("Sandra@demo.com")
.build());
customers.add(Customer.builder()
.firstName("Miller").lastName("Gerty")
.age(41).email("miller@demo.com")
.build());
customers.add(Customer.builder()
.firstName("Milton").lastName("Arnison")
.age(30).email("arnison@demo.com")
.build());
customers.add(Customer.builder()
.firstName("Crissy").lastName("Finch")
.age(35).email("crissy@demo.com")
.build());
return customers;
}
}

View File

@@ -0,0 +1,16 @@
package com.javadevjournal.data;
import lombok.Builder;
import lombok.Data;
import java.io.Serializable;
@Data
@Builder
public class Customer implements Serializable {
private String firstName;
private String lastName;
private String email;
private int age;
}