Comparator - Java 8

 Comparator interface now have multiple functions which will helps us to have multiple comparison together. see below

  1. Comparator.comparing() : static method which takes comparator to compare
  2. thenComparaing(): use it when you want to have another comparator to compare
  3. thenComparingInt, thenComparingDouble, thenComparingLong : specialized for primitive types.


Example: 

 

In the given example we have made flexibility to sort the employee List by firstName, lastName or Salary and sorting order can be asc/desc


import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class ComparatorTest
{
public static void main(String[] args)
{
ArrayList<Employee> employees = getUnsortedEmployeeList();

//Compare by first name and then last name
Comparator<Employee> compareByName = getCompareBy("salary");

System.out.println(getSortedList(employees, "asc", compareByName));
}

static Comparator<Employee> getCompareBy(String field)
{
if(field.equalsIgnoreCase("salary"))
{

return Comparator.comparing((Employee e1)->e1.getSalary().getValue())
.thenComparing(Employee::getFirstName)
.thenComparing(Employee::getLastName);
}
else if (field.equalsIgnoreCase("lastName"))
{
return Comparator
.comparing(Employee::getLastName)
.thenComparing(Employee::getFirstName)
.thenComparing((Employee e1)->e1.getSalary().getValue());
}
return Comparator
.comparing(Employee::getFirstName)
.thenComparing(Employee::getLastName)
.thenComparing((Employee e1)->e1.getSalary().getValue());
}

static List<Employee> getSortedList(ArrayList<Employee> employees, String order, Comparator<Employee> compareByName)
{
if(order.equalsIgnoreCase("desc"))
{
compareByName = compareByName.reversed();
}
return employees.stream()
.sorted(compareByName)
.collect(Collectors.toList());
}
private static ArrayList<Employee> getUnsortedEmployeeList()
{
ArrayList<Employee> list = new ArrayList<>();
list.add( new Employee(new Salary(new BigDecimal(500000)), "Bipin", "Gupta"));
list.add( new Employee(new Salary(new BigDecimal(800000)), "Umesh", "Mehta"));
list.add( new Employee(new Salary(new BigDecimal(600000)), "Ramesh", "Dangi"));
list.add( new Employee(new Salary(new BigDecimal(1000000)), "Atin", "Kapoor"));
list.add( new Employee(new Salary(new BigDecimal(800000)), "Aman", "Verma"));
list.add( new Employee(new Salary(new BigDecimal(550000)), "Rahi", "Akela"));
list.add( new Employee(new Salary(new BigDecimal(500000)), "Dinesh", "Mehta"));
return list;
}
}

public class Employee {

private Salary salary;
private String firstName;
private String lastName;

public Employee(Salary salary, String firstName, String lastName) {
this.salary = salary;
this.firstName = firstName;
this.lastName = lastName;
}

public Salary getSalary() {
return salary;
}

public void setSalary(Salary salary) {
this.salary = salary;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
lastName = lastName;
}

@Override
public String toString() {
return firstName+":"+lastName+":"+ salary.getValue().toString();
}
}

import java.math.BigDecimal;

public class Salary
{
private BigDecimal value;

public Salary(BigDecimal i) {
this.value = i;
}

public BigDecimal getValue() {
return value;
}

public void setValue(BigDecimal value) {
this.value = value;
}
}


Output:
[Bipin:Gupta:500000, Dinesh:Mehta:500000, Rahi:Akela:550000, Ramesh:Dangi:600000, Aman:Verma:800000, Umesh:Mehta:800000, Atin:Kapoor:1000000]

Comments

Popular posts from this blog

How to pass key-value parameter on command line - JAVA

Java Installation on Windows System