Java 8 Comparator with lambda
This commit is contained in:
23
Java/comparator-lambda-expression/pom.xml
Normal file
23
Java/comparator-lambda-expression/pom.xml
Normal 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>
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user