Skip to main content

Posts

Showing posts with the label collection

Collection of user defined comparable objects (Part 2)

Yesterday we discussed how to make user defined objects comparable to themselves . Let’s take that to one step further today. In yesterdays post, our requirement was to sort the collection of employees that belong to a division, by employee id, so that the end result will be a collection of employees in seniority order. Now there comes the payroll division and it says, for each division, you need to print the list of employees based on the salary drawn. You may say “It is very simple; I had done all the ground work yesterday; so just need to change the comparison logic as below”. public class Employee implements Comparable<Employee> { private String name; private int employeeNo; private float salary; public int compareTo(Employee e) { if (salary == e.salary) { return 0; } else if (salary > e.salary) { return 1; } else { return -1; } } } Now the human resources division shouts “What have you done? Now e...