All advanced sorting techniques using Java 8 and Streams

package com.dpq.interview.Q;

import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

public class SortingDemo {
public static void main(String[] args) throws ParseException {
allSortingTechniquesAfterJava8();
}

public static void allSortingTechniquesAfterJava8() throws ParseException {
List<Employee> list = populateList();
System.out.println(“####################################### Natiral soring by Employee Id ###############################################”);
list.stream().sorted().forEach(e -> System.out.println(e));
System.out.println(“####################################### Natiral soring by Employee Id but in desending order ###############################################”);
list.stream().sorted(Collections.reverseOrder()).forEach(e -> System.out.println(e));
List<Employee> sortedList = list.stream().sorted(Collections.reverseOrder()).toList();
System.out.println(“Sorting in decending order: “+ sortedList);

System.out.println(“####################################### soring by comparator ###############################################”);
List<Emp> empList = populateEmpList();
empList.sort((o1,o2) -> o1.getEmployeeName().toLowerCase().compareTo(o2.getEmployeeName().toLowerCase()));

empList.forEach(System.out::println);
System.out.println(“####################################### soring by comparator another way ###############################################”);
List<Emp> empList1 = populateEmpList();
empList1.sort(Comparator.comparing(Emp::getEmployeeName).reversed());
empList1.forEach(System.out::println);

System.out.println(“####################################### soring by comparator by Date ###############################################”);
empList1.sort(Comparator.comparing(Emp::getDateOfJoining));
empList1.forEach(System.out::println);

System.out.println(“####################################### soring by comparator by Address ###############################################”);
empList1.sort(Comparator.comparing(t -> ((Emp)t).getEmployeeAddress().getEmployeeAddress()));
empList1.forEach(System.out::println);

System.out.println(“####################################### soring by comparator by Address another way ###############################################”);
empList1.sort((o1,o2) -> o1.getEmployeeAddress().getEmployeeAddress().compareTo(o2.getEmployeeAddress().getEmployeeAddress()));
empList1.forEach(System.out::println);

System.out.println(“####################################### soring by comparator by Address another way ###############################################”);
System.out.println(empList1.parallelStream().anyMatch(e -> e.getEmployeeName().equalsIgnoreCase(“deepak”))); //true
System.out.println(empList1.parallelStream().allMatch(e -> e.getEmployeeName().equalsIgnoreCase(“deepak”))); //false

new Thread( ()-> {
System.out.println(Thread.currentThread());
}).start();;

new Thread( ()-> {
System.out.println(Thread.currentThread());
}).start();;

new Thread( ()-> {
System.out.println(Thread.currentThread());
}).start();;
}

public static List<Emp> populateEmpList() throws ParseException{
List<Emp> list = new ArrayList<>();

list.add(new Emp(50,”deepak”,new SimpleDateFormat(“dd-MM-yyyy”).parse(“14-06-1984”),new BigInteger(“150000″),new EmployeeAddress(50,”Gwalior”)));
list.add(new Emp(40,”DPQ”,new SimpleDateFormat(“dd-MM-yyyy”).parse(“14-06-1984”),new BigInteger(“130000″),new EmployeeAddress(40,”Gwalior”)));
list.add(new Emp(10,”Shiob”,new SimpleDateFormat(“dd-MM-yyyy”).parse(“12-06-1984”),new BigInteger(“250000″),new EmployeeAddress(10,”Hydrabad”)));
list.add(new Emp(20,”Himanshu”,new SimpleDateFormat(“dd-MM-yyyy”).parse(“14-06-2000”),new BigInteger(“50000″),new EmployeeAddress(20,”Indore”)));
list.add(new Emp(30,”Gagan”,new SimpleDateFormat(“dd-MM-yyyy”).parse(“30-06-1982”),new BigInteger(“15000″),new EmployeeAddress(30,”UK”)));
return list;
}

public static List<Employee> populateList() throws ParseException{
List<Employee> list = new ArrayList<>();
list.add(new Employee(50,”deepak”,new SimpleDateFormat(“dd-MM-yyyy”).parse(“14-06-1984”),new BigInteger(“150000″),new Address(50,”Gwalior”)));
list.add(new Employee(40,”DPQ”,new SimpleDateFormat(“dd-MM-yyyy”).parse(“14-06-1984”),new BigInteger(“130000″),new Address(40,”Gwalior”)));
list.add(new Employee(10,”Shiob”,new SimpleDateFormat(“dd-MM-yyyy”).parse(“12-06-1984”),new BigInteger(“250000″),new Address(10,”Hydrabad”)));
list.add(new Employee(20,”Himanshu”,new SimpleDateFormat(“dd-MM-yyyy”).parse(“14-06-2000”),new BigInteger(“50000″),new Address(20,”Indore”)));
list.add(new Employee(30,”Gagan”,new SimpleDateFormat(“dd-MM-yyyy”).parse(“30-06-1982”),new BigInteger(“15000″),new Address(30,”UK”)));
return list;
}

public static void allSortingTechniquesBeforeJava8() throws ParseException {
List<Employee> list = populateList();
System.out.println(“################################################ Before Sorting #################################################”);
System.out.println(list);
System.out.println(“################################################ After Sorting by employeeId using comparable and compareTo method #################################################”);
Collections.sort(list);
System.out.println(list);
System.out.println(“################################################ Sorting based on SortByEmployeeDateOfJoining #################################################”);
Collections.sort(list,new SortByEmployeeDateOfJoining());
System.out.println(list);
System.out.println(“################################################ Sorting based on SortByEmployeeName #################################################”);
Collections.sort(list,new SortByEmployeeName());
System.out.println(list);
System.out.println(“################################################ Sorting based on SortByEmployeeSalary #################################################”);
Collections.sort(list,new SortByEmployeeSalary());
System.out.println(list);
System.out.println(“################################################ Sorting based on SortByEmployeeId from Address #################################################”);
Collections.sort(list,new SortByEmployeeAddressID());
System.out.println(list);

System.out.println(“################################################ Sorting based on SortByEmployee Address #################################################”);
Collections.sort(list,new SortByEmployeeAddress());
System.out.println(list);
}
}

class Employee implements Comparable<Employee>{
private Integer employeeId;
private String employeeName;
private Date dateOfJoining;
private BigInteger salary;
private Address employeeAddress;

public Employee(Integer employeeId, String employeeName, Date dateOfJoining, BigInteger salary,
Address employeeAddress) {
super();
this.employeeId = employeeId;
this.employeeName = employeeName;
this.dateOfJoining = dateOfJoining;
this.salary = salary;
this.employeeAddress = employeeAddress;
}

@Override
public int compareTo(Employee o) {
// TODO Auto-generated method stub
return employeeId – o.employeeId;
}

public Integer getEmployeeId() {
return employeeId;
}
public String getEmployeeName() {
return employeeName;
}
public Date getDateOfJoining() {
return dateOfJoining;
}
public BigInteger getSalary() {
return salary;
}
public Address getEmployeeAddress() {
return employeeAddress;
}
@Override
public String toString() {
return “Employee [employeeId=” + employeeId + “, employeeName=” + employeeName + “, dateOfJoining=”
+ dateOfJoining + “, salary=” + salary + “, employeeAddress=” + employeeAddress + “]”;
}
}

class Emp{
private Integer employeeId;
private String employeeName;
private Date dateOfJoining;
private BigInteger salary;
private EmployeeAddress employeeAddress;

public Emp(Integer employeeId, String employeeName, Date dateOfJoining, BigInteger salary,
EmployeeAddress employeeAddress) {
super();
this.employeeId = employeeId;
this.employeeName = employeeName;
this.dateOfJoining = dateOfJoining;
this.salary = salary;
this.employeeAddress = employeeAddress;
}

public Integer getEmployeeId() {
return employeeId;
}
public String getEmployeeName() {
return employeeName;
}
public Date getDateOfJoining() {
return dateOfJoining;
}
public BigInteger getSalary() {
return salary;
}
public EmployeeAddress getEmployeeAddress() {
return employeeAddress;
}
@Override
public String toString() {
return “Employee [employeeId=” + employeeId + “, employeeName=” + employeeName + “, dateOfJoining=”
+ dateOfJoining + “, salary=” + salary + “, employeeAddress=” + employeeAddress + “]”;
}
}

class SortByEmployeeName implements Comparator<Employee>{
@Override
public int compare(Employee o1, Employee o2) {
return o1.getEmployeeName().compareTo(o2.getEmployeeName());
}
}

class SortByEmployeeDateOfJoining implements Comparator<Employee>{
@Override
public int compare(Employee o1, Employee o2) {
return o1.getDateOfJoining().compareTo(o2.getDateOfJoining());
}
}

class SortByEmployeeSalary implements Comparator<Employee>{
@Override
public int compare(Employee o1, Employee o2) {
return o1.getSalary().compareTo(o2.getSalary());
}
}

class SortByEmployeeAddressID implements Comparator<Employee>{
@Override
public int compare(Employee o1, Employee o2) {
return o1.getEmployeeAddress().compareTo(o2.getEmployeeAddress());
}
}

class SortByEmployeeAddress implements Comparator<Employee>{
@Override
public int compare(Employee o1, Employee o2) {
return o1.getEmployeeAddress().getEmployeeAddress().compareTo(o2.getEmployeeAddress().getEmployeeAddress());
}
}

class Address implements Comparable<Address>{
private Integer employeeId;
private String employeeAddress;

public Address(Integer employeeId, String employeeAddress) {
super();
this.employeeId = employeeId;
this.employeeAddress = employeeAddress;
}

@Override
public int compareTo(Address address) {
// TODO Auto-generated method stub
return employeeId – address.employeeId;
}

public Integer getEmployeeId() {
return employeeId;
}

public String getEmployeeAddress() {
return employeeAddress;
}

@Override
public String toString() {
return “Address [employeeId=” + employeeId + “, employeeAddress=” + employeeAddress + “]”;
}
}

class EmployeeAddress{
private Integer employeeId;
private String employeeAddress;

public EmployeeAddress(Integer employeeId, String employeeAddress) {
super();
this.employeeId = employeeId;
this.employeeAddress = employeeAddress;
}

public Integer getEmployeeId() {
return employeeId;
}

public String getEmployeeAddress() {
return employeeAddress;
}

@Override
public String toString() {
return “Address [employeeId=” + employeeId + “, employeeAddress=” + employeeAddress + “]”;
}
}

Output:

####################################### Natiral soring by Employee Id ###############################################

Employee [employeeId=10, employeeName=Shiob, dateOfJoining=Tue Jun 12 00:00:00 IST 1984, salary=250000, employeeAddress=Address [employeeId=10, employeeAddress=Hydrabad]]

Employee [employeeId=20, employeeName=Himanshu, dateOfJoining=Wed Jun 14 00:00:00 IST 2000, salary=50000, employeeAddress=Address [employeeId=20, employeeAddress=Indore]]

Employee [employeeId=30, employeeName=Gagan, dateOfJoining=Wed Jun 30 00:00:00 IST 1982, salary=15000, employeeAddress=Address [employeeId=30, employeeAddress=UK]]

Employee [employeeId=40, employeeName=DPQ, dateOfJoining=Thu Jun 14 00:00:00 IST 1984, salary=130000, employeeAddress=Address [employeeId=40, employeeAddress=Gwalior]]

Employee [employeeId=50, employeeName=deepak, dateOfJoining=Thu Jun 14 00:00:00 IST 1984, salary=150000, employeeAddress=Address [employeeId=50, employeeAddress=Gwalior]]

####################################### Natiral soring by Employee Id but in desending order ###############################################

Employee [employeeId=50, employeeName=deepak, dateOfJoining=Thu Jun 14 00:00:00 IST 1984, salary=150000, employeeAddress=Address [employeeId=50, employeeAddress=Gwalior]]

Employee [employeeId=40, employeeName=DPQ, dateOfJoining=Thu Jun 14 00:00:00 IST 1984, salary=130000, employeeAddress=Address [employeeId=40, employeeAddress=Gwalior]]

Employee [employeeId=30, employeeName=Gagan, dateOfJoining=Wed Jun 30 00:00:00 IST 1982, salary=15000, employeeAddress=Address [employeeId=30, employeeAddress=UK]]

Employee [employeeId=20, employeeName=Himanshu, dateOfJoining=Wed Jun 14 00:00:00 IST 2000, salary=50000, employeeAddress=Address [employeeId=20, employeeAddress=Indore]]

Employee [employeeId=10, employeeName=Shiob, dateOfJoining=Tue Jun 12 00:00:00 IST 1984, salary=250000, employeeAddress=Address [employeeId=10, employeeAddress=Hydrabad]]

Sorting in decending order: [Employee [employeeId=50, employeeName=deepak, dateOfJoining=Thu Jun 14 00:00:00 IST 1984, salary=150000, employeeAddress=Address [employeeId=50, employeeAddress=Gwalior]], Employee [employeeId=40, employeeName=DPQ, dateOfJoining=Thu Jun 14 00:00:00 IST 1984, salary=130000, employeeAddress=Address [employeeId=40, employeeAddress=Gwalior]], Employee [employeeId=30, employeeName=Gagan, dateOfJoining=Wed Jun 30 00:00:00 IST 1982, salary=15000, employeeAddress=Address [employeeId=30, employeeAddress=UK]], Employee [employeeId=20, employeeName=Himanshu, dateOfJoining=Wed Jun 14 00:00:00 IST 2000, salary=50000, employeeAddress=Address [employeeId=20, employeeAddress=Indore]], Employee [employeeId=10, employeeName=Shiob, dateOfJoining=Tue Jun 12 00:00:00 IST 1984, salary=250000, employeeAddress=Address [employeeId=10, employeeAddress=Hydrabad]]]

#######################################  soring by comparator ###############################################

Employee [employeeId=50, employeeName=deepak, dateOfJoining=Thu Jun 14 00:00:00 IST 1984, salary=150000, employeeAddress=Address [employeeId=50, employeeAddress=Gwalior]]

Employee [employeeId=40, employeeName=DPQ, dateOfJoining=Thu Jun 14 00:00:00 IST 1984, salary=130000, employeeAddress=Address [employeeId=40, employeeAddress=Gwalior]]

Employee [employeeId=30, employeeName=Gagan, dateOfJoining=Wed Jun 30 00:00:00 IST 1982, salary=15000, employeeAddress=Address [employeeId=30, employeeAddress=UK]]

Employee [employeeId=20, employeeName=Himanshu, dateOfJoining=Wed Jun 14 00:00:00 IST 2000, salary=50000, employeeAddress=Address [employeeId=20, employeeAddress=Indore]]

Employee [employeeId=10, employeeName=Shiob, dateOfJoining=Tue Jun 12 00:00:00 IST 1984, salary=250000, employeeAddress=Address [employeeId=10, employeeAddress=Hydrabad]]

#######################################  soring by comparator another way ###############################################

Employee [employeeId=50, employeeName=deepak, dateOfJoining=Thu Jun 14 00:00:00 IST 1984, salary=150000, employeeAddress=Address [employeeId=50, employeeAddress=Gwalior]]

Employee [employeeId=10, employeeName=Shiob, dateOfJoining=Tue Jun 12 00:00:00 IST 1984, salary=250000, employeeAddress=Address [employeeId=10, employeeAddress=Hydrabad]]

Employee [employeeId=20, employeeName=Himanshu, dateOfJoining=Wed Jun 14 00:00:00 IST 2000, salary=50000, employeeAddress=Address [employeeId=20, employeeAddress=Indore]]

Employee [employeeId=30, employeeName=Gagan, dateOfJoining=Wed Jun 30 00:00:00 IST 1982, salary=15000, employeeAddress=Address [employeeId=30, employeeAddress=UK]]

Employee [employeeId=40, employeeName=DPQ, dateOfJoining=Thu Jun 14 00:00:00 IST 1984, salary=130000, employeeAddress=Address [employeeId=40, employeeAddress=Gwalior]]

#######################################  soring by comparator by Date ###############################################

Employee [employeeId=30, employeeName=Gagan, dateOfJoining=Wed Jun 30 00:00:00 IST 1982, salary=15000, employeeAddress=Address [employeeId=30, employeeAddress=UK]]

Employee [employeeId=10, employeeName=Shiob, dateOfJoining=Tue Jun 12 00:00:00 IST 1984, salary=250000, employeeAddress=Address [employeeId=10, employeeAddress=Hydrabad]]

Employee [employeeId=50, employeeName=deepak, dateOfJoining=Thu Jun 14 00:00:00 IST 1984, salary=150000, employeeAddress=Address [employeeId=50, employeeAddress=Gwalior]]

Employee [employeeId=40, employeeName=DPQ, dateOfJoining=Thu Jun 14 00:00:00 IST 1984, salary=130000, employeeAddress=Address [employeeId=40, employeeAddress=Gwalior]]

Employee [employeeId=20, employeeName=Himanshu, dateOfJoining=Wed Jun 14 00:00:00 IST 2000, salary=50000, employeeAddress=Address [employeeId=20, employeeAddress=Indore]]

#######################################  soring by comparator by Address ###############################################

Employee [employeeId=50, employeeName=deepak, dateOfJoining=Thu Jun 14 00:00:00 IST 1984, salary=150000, employeeAddress=Address [employeeId=50, employeeAddress=Gwalior]]

Employee [employeeId=40, employeeName=DPQ, dateOfJoining=Thu Jun 14 00:00:00 IST 1984, salary=130000, employeeAddress=Address [employeeId=40, employeeAddress=Gwalior]]

Employee [employeeId=10, employeeName=Shiob, dateOfJoining=Tue Jun 12 00:00:00 IST 1984, salary=250000, employeeAddress=Address [employeeId=10, employeeAddress=Hydrabad]]

Employee [employeeId=20, employeeName=Himanshu, dateOfJoining=Wed Jun 14 00:00:00 IST 2000, salary=50000, employeeAddress=Address [employeeId=20, employeeAddress=Indore]]

Employee [employeeId=30, employeeName=Gagan, dateOfJoining=Wed Jun 30 00:00:00 IST 1982, salary=15000, employeeAddress=Address [employeeId=30, employeeAddress=UK]]

#######################################  soring by comparator by Address another way ###############################################

Employee [employeeId=50, employeeName=deepak, dateOfJoining=Thu Jun 14 00:00:00 IST 1984, salary=150000, employeeAddress=Address [employeeId=50, employeeAddress=Gwalior]]

Employee [employeeId=40, employeeName=DPQ, dateOfJoining=Thu Jun 14 00:00:00 IST 1984, salary=130000, employeeAddress=Address [employeeId=40, employeeAddress=Gwalior]]

Employee [employeeId=10, employeeName=Shiob, dateOfJoining=Tue Jun 12 00:00:00 IST 1984, salary=250000, employeeAddress=Address [employeeId=10, employeeAddress=Hydrabad]]

Employee [employeeId=20, employeeName=Himanshu, dateOfJoining=Wed Jun 14 00:00:00 IST 2000, salary=50000, employeeAddress=Address [employeeId=20, employeeAddress=Indore]]

Employee [employeeId=30, employeeName=Gagan, dateOfJoining=Wed Jun 30 00:00:00 IST 1982, salary=15000, employeeAddress=Address [employeeId=30, employeeAddress=UK]]

#######################################  soring by comparator by Address another way ###############################################

true

false

Thread[Thread-0,5,main]

Thread[Thread-1,5,main]

Thread[Thread-2,5,main]

Popular posts from this blog

How to change column name in Dataframe and selection of few columns in Dataframe using Pyspark with example

What is Garbage collection in Spark and its impact and resolution

Window function in PySpark with Joins example using 2 Dataframes (inner join)