Home > OS >  How do I use comparable method with if conditions when the two types of data are different?
How do I use comparable method with if conditions when the two types of data are different?

Time:06-11

I have to write an Employee class that has a comparable method that will be used to sort an ArrayList of employees. First it compares the number of years the employee has been working with another employee number of years, if the number of years are the same then it moves on to comparing salary, both conditions should sort in ascending order. My issue is I get an incompatible type error since salary is a double data type, is there anything I can do?

public class Employee implements Comparable<Employee>
{
    private String lastName;
    private String firstName;
    private int years;
    private double salary;

    public Employee(String lastName, String firstName, int years, double salary)
    {
        this.lastName=lastName;
        this.firstName=firstName;
        this.years=years;
        this.salary=salary;
    }

    public void setLastName(String newlastName)
    {
        lastName=newlastName;
    }
    public String getLastName()
    {
        return lastName;
    }


    public void setFirstName(String newfirstName)
    {
        firstName=newfirstName;
    }
    public String getFirstName()
    {
        return firstName;
    }


    public void setYears(int newyears)
    {
        years=newyears;
    }
    public int getYears()
    {
        return years;
    }


    public void setSalary(double newsalary)
    {
        salary=newsalary;
    }
    public double getSalary()
    {
        return salary;
    }

    public String toString()
    {
        String s="" lastName "-" firstName ":" years ":" salary;
        return s;
    }

    public int compareTo(Employee that)
    {
        if(this.years != that.getYears())
        {
            return this.years - that.getYears();
        }
        else
        {
            return this.salary - that.getSalary();
        }
    }
}

CodePudding user response:

this.salary - that.getSalary() results in a double, but the compareTo() function needs to return an int. In general, Java doesn't like to implicitly convert a double to an int because that results in a loss of information (I presume the error said something about a "lossy conversion").

One way to implement a compareTo() that uses double variables to compare two objects is to manually return -1, 0, or 1 depending on the comparison:

if (this.salary < that.getSalary())
    return -1;
else if (this.salary > that.getSalary())
    return 1;
return 0;

CodePudding user response:

I don't like your implementation of compareTo.

I would implement equals and hashCode properly.

If you must implement Comparable, it should be consistent with equals.

I would not have the Employee class implement Comparable. You can do that salary/age comparison with a lambda.

CodePudding user response:

Sadly, there is no way to allow the compareTo method to return something other than an int. Luckily, there is a workaround!

Comparables do not need to return the exact difference between the two inputs, just an indication as to which one is larger.

i.e. you don't need to return anything other than 1, 0, and -1.

Therefore, Math.signum() can come to the rescue!

Math.signum(double d) returns the sign of d, or 0 if d == 0.

More info on Math.signum() at geeksforgeeks: https://www.geeksforgeeks.org/java-signum-method-examples/

There is a version of signum for integers as well: Integer.signum(int i)

It would look something like this in your code:

public int compareTo(Employee that)
{
    if(this.years != that.getYears())
    {
        return Integer.signum(this.years - that.getYears());
    }
    else
    {
        return (int) Math.signum(this.salary - that.getSalary());
    }
}

Note that we must cast Math.signum(this.salary - that.getSalary()) to an int because it returns a double

  • Related