Sorting using Comparable by a given object instance

Reading Time: 3 minutes

In my school work I needed to implement such a method which can sort my date instance by hour which is set in it. I kept those many instance of the regarding class in a treeset sorted by hour, So the below is example shows that randomly added instances into the treeset is sorted in the treeset which adds up the values in a given sequence whereas I’ve provided the compare method. as you see below in the domain class

Also for  your information comparable interface is implemented. By the way don’t you get confused with the dao interface, below you’ll find the custom date format method

Regarding Method

@Override
    public Date dateTimeGiven(Integer hour, Integer minute, Integer second) {

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minute);
        calendar.set(Calendar.SECOND, second);
        Date date = calendar.getTime();
        return date;
    }

Domain class 

import java.util.Date;
import java.util.Objects;

public class Appointment implements Comparable<Appointment> {

    private Patient patient;
    private String specialist;
    private Date appointmentDate;
    private String slot;
    private String appointmentStatus;

    public Appointment(Date appointmentDate) {
        this.appointmentDate = appointmentDate;
    }
    
    

    public Appointment() {

    }

    public Appointment(Patient patient, String specialist, Date appointmentDate, String slot, String appointmentStatus) {
        this.patient = patient;
        this.specialist = specialist;
        this.appointmentDate = appointmentDate;
        this.slot = slot;
        this.appointmentStatus = appointmentStatus;
    }

    public Patient getPatient() {
        return patient;
    }

    public void setPatient(Patient patient) {
        this.patient = patient;
    }

    public String getSpecialist() {
        return specialist;
    }

    public void setSpecialist(String specialist) {
        this.specialist = specialist;
    }

    public Date getAppointmentDate() {
        return appointmentDate;
    }

    public void setAppointmentDate(Date appointmentDate) {
        this.appointmentDate = appointmentDate;
    }

    public String getSlot() {
        return slot;
    }

    public void setSlot(String slot) {
        this.slot = slot;
    }

    public String getAppointmentStatus() {
        return appointmentStatus;
    }

    public void setAppointmentStatus(String appointmentStatus) {
        this.appointmentStatus = appointmentStatus;
    }

    @Override
    public String toString() {
        return "Appointment{" + "patient=" + patient + ", specialist=" + specialist + ", appointmentDate=" + appointmentDate + ", slot=" + slot + ", appointmentStatus=" + appointmentStatus + '}';
    }

    @Override
    public int hashCode() {
        int hash = 7;
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Appointment other = (Appointment) obj;
        if (!Objects.equals(this.patient, other.patient)) {
            return false;
        }
        if (!Objects.equals(this.specialist, other.specialist)) {
            return false;
        }
        if (!Objects.equals(this.appointmentDate, other.appointmentDate)) {
            return false;
        }
        if (!Objects.equals(this.slot, other.slot)) {
            return false;
        }
        if (!Objects.equals(this.appointmentStatus, other.appointmentStatus)) {
            return false;
        }
        return true;
    }

    @Override
    public int compareTo(Appointment o) {
        return getAppointmentDate().compareTo(o.getAppointmentDate());
    }

}

Main class

import com.tugrulaslan.dao.HospitalDAO;
import com.tugrulaslan.dao.HospitalDAOImpl;
import com.tugrulaslan.model.Appointment;
import java.util.Date;
import java.util.Set;
import java.util.TreeSet;

/**
 *
 * @author Tugrul
 */
public class TestMain {

    public static void main(String[] args) {
        HospitalDAO hospitalDAO = new HospitalDAOImpl();
        Date date1 = hospitalDAO.dayMonthTimeGiven(9, 0, 0);
        Date date2 = hospitalDAO.dayMonthTimeGiven(10, 0, 0);
        Date date3 = hospitalDAO.dayMonthTimeGiven(11, 0, 0);
        Date date4 = hospitalDAO.dayMonthTimeGiven(12, 0, 0);
        Date date5 = hospitalDAO.dayMonthTimeGiven(13, 0, 0);
        Date date6 = hospitalDAO.dayMonthTimeGiven(14, 0, 0);
        Date date7 = hospitalDAO.dayMonthTimeGiven(15, 0, 0);
        Date date8 = hospitalDAO.dayMonthTimeGiven(16, 0, 0);
        Date date9 = hospitalDAO.dayMonthTimeGiven(17, 0, 0);

        Appointment appointment1 = new Appointment(date1);
        Appointment appointment2 = new Appointment(date2);
        Appointment appointment3 = new Appointment(date3);
        Appointment appointment4 = new Appointment(date4);
        Appointment appointment5 = new Appointment(date5);
        Appointment appointment6 = new Appointment(date6);
        Appointment appointment7 = new Appointment(date7);
        Appointment appointment8 = new Appointment(date8);
        Appointment appointment9 = new Appointment(date9);

        Set<Appointment> setList = new TreeSet<>();

        setList.add(appointment4);
        setList.add(appointment5);
        setList.add(appointment6);
        setList.add(appointment1);
        setList.add(appointment2);
        setList.add(appointment7);
        setList.add(appointment3);
        setList.add(appointment9);
        setList.add(appointment8);
        int i = 1;
        for (Appointment obj : setList) {
            System.out.println("Object " + i + " date value " + obj.getAppointmentDate());
            i++;
        }
    }

}

Output

Object 1 date value Fri Jan 09 00:42:33 EET 2015
Object 2 date value Sat Jan 10 00:42:33 EET 2015
Object 3 date value Sun Jan 11 00:42:33 EET 2015
Object 4 date value Mon Jan 12 00:42:33 EET 2015
Object 5 date value Tue Jan 13 00:42:33 EET 2015
Object 6 date value Wed Jan 14 00:42:33 EET 2015
Object 7 date value Thu Jan 15 00:42:33 EET 2015
Object 8 date value Fri Jan 16 00:42:33 EET 2015
Object 9 date value Sat Jan 17 00:42:33 EET 2015