Site icon

Sort user defined objects using TreeSet in java (Comparator interface)

What is TreeSet?

Example – Sort POJO or user defined object using comparator interface.

Given a TreeSet containing Vehicle objects. Vehicle class has following data members.

We would like to Sort Vehicle objects by model, owner and serviceDate. We will have a specialized Comparator for each sorting need. We will have following Comparators:

  1. Comparator to sort Vehicle objects by model
  2. Comparator to sort Vehicle objects by owner
  3. Comparator to sort Vehicle objects by serviceDate

Program – sort user defined objects contained in a TreeSet using java

package org.learn.collection.set.tset;

import java.time.LocalDate;
import java.time.Month;
import java.util.Comparator;
import java.util.TreeSet;

class Vehicle {
	public String model;	
	public String owner;
	public String registerCity;
	public LocalDate serviceDate;

	public Vehicle(String model,String owner, String registerCity, LocalDate serviceDate) {
		this.model = model;
		this.serviceDate = serviceDate;
		this.owner = owner;
		this.registerCity = registerCity;
	}

	public String toString() {
		 return String.format("[Model:%s,Owner:%s,registeredCity:%s,ServiceDate:%s]", 
				 model,owner,registerCity,serviceDate.toString());
	 }
}

class SortByModel implements Comparator<Vehicle> {

	@Override
	public int compare(Vehicle vehicle1, Vehicle vehicle2) {
		return vehicle1.model.compareTo(vehicle2.model);
	}
}

class SortByOwner implements Comparator<Vehicle> {
	@Override
	public int compare(Vehicle vehicle1, Vehicle vehicle2) {
		return vehicle1.owner.compareTo(vehicle2.owner);
	}
}

class SortByServiceDate implements Comparator<Vehicle> {

	@Override
	public int compare(Vehicle vehicle1, Vehicle vehicle2) {
		return vehicle1.serviceDate.compareTo(vehicle2.serviceDate);
	}
}

public class DemoTreeSetComparator {

	public static void main(String[] args) {
		TreeSet<Vehicle> vehicleList = new TreeSet<Vehicle>(new SortByModel());
		vehicleList.add(new Vehicle("S-Class", "Guzan", "NewYork", LocalDate.of(2014, Month.AUGUST, 01)));
		vehicleList.add(new Vehicle("C-Class", "Bedoya", "California", LocalDate.of(2009, Month.DECEMBER, 05)));
		vehicleList.add(new Vehicle("A-Class", "Cameron", "Illinois", LocalDate.of(2014, Month.JULY, 25)));
		vehicleList.add(new Vehicle("SL-Class", "Johnson", "Kansas" ,LocalDate.of(2011, Month.DECEMBER, 10)));
		System.out.println("1. Sorted Vehicle list by Model:\n"+ vehicleList);
		
		vehicleList = new TreeSet<Vehicle>(new SortByOwner());
		vehicleList.add(new Vehicle("S-Class", "Guzan", "NewYork", LocalDate.of(2014, Month.AUGUST, 01)));
		vehicleList.add(new Vehicle("C-Class", "Bedoya", "California", LocalDate.of(2009, Month.DECEMBER, 05)));
		vehicleList.add(new Vehicle("A-Class", "Cameron", "Illinois", LocalDate.of(2014, Month.JULY, 25)));
		vehicleList.add(new Vehicle("SL-Class", "Johnson", "Kansas" ,LocalDate.of(2011, Month.DECEMBER, 10)));
		System.out.println("\n2. Sorted Vehicle list by Owner:\n"+ vehicleList);
		
		vehicleList = new TreeSet<Vehicle>(new SortByServiceDate());
		vehicleList.add(new Vehicle("S-Class", "Guzan", "NewYork", LocalDate.of(2014, Month.AUGUST, 01)));
		vehicleList.add(new Vehicle("C-Class", "Bedoya", "California", LocalDate.of(2009, Month.DECEMBER, 05)));
		vehicleList.add(new Vehicle("A-Class", "Cameron", "Illinois", LocalDate.of(2014, Month.JULY, 25)));
		vehicleList.add(new Vehicle("SL-Class", "Johnson", "Kansas" ,LocalDate.of(2011, Month.DECEMBER, 10)));
		System.out.println("\n3. Sorted Vehicle list by service date:\n"+ vehicleList);
		
	}
}

Output – sort user defined objects contained in a TreeSet using java

1. Sorted Vehicle list by Model:
[
[Model:A-Class,Owner:Cameron,registeredCity:Illinois,ServiceDate:2014-07-25], 
[Model:C-Class,Owner:Bedoya,registeredCity:California,ServiceDate:2009-12-05], 
[Model:S-Class,Owner:Guzan,registeredCity:NewYork,ServiceDate:2014-08-01], 
[Model:SL-Class,Owner:Johnson,registeredCity:Kansas,ServiceDate:2011-12-10]]

2. Sorted Vehicle list by Owner:
[
[Model:C-Class,Owner:Bedoya,registeredCity:California,ServiceDate:2009-12-05], 
[Model:A-Class,Owner:Cameron,registeredCity:Illinois,ServiceDate:2014-07-25], 
[Model:S-Class,Owner:Guzan,registeredCity:NewYork,ServiceDate:2014-08-01], 
[Model:SL-Class,Owner:Johnson,registeredCity:Kansas,ServiceDate:2011-12-10]]

3. Sorted Vehicle list by service date:
[
[Model:C-Class,Owner:Bedoya,registeredCity:California,ServiceDate:2009-12-05], 
[Model:SL-Class,Owner:Johnson,registeredCity:Kansas,ServiceDate:2011-12-10], 
[Model:A-Class,Owner:Cameron,registeredCity:Illinois,ServiceDate:2014-07-25], 
[Model:S-Class,Owner:Guzan,registeredCity:NewYork,ServiceDate:2014-08-01]]
Exit mobile version