When we use the comparable or comparator interfaces, why do we have to make it compatible with the equals method and how to do it?


Apparently it works well for me, but in the information I've read, I recommend that the compareTo() method defined by the Comparable() interface be compatible with the equals() method. This means that: objeto1.compareTo(objeto2)==0 when objeto1.equals(objeto2)==true .

I do not know exactly what I have to do but without doing any equals works for me.

I have this method to call it:

public void ordenarPorNombreComparableComparator() {


package conArrayList_Vector_Comparable_comparator;  
    public abstract class Empleado implements Comparable<Empleado>{
        static int autonumerico;
        private String nombre;
        private int edad;
        private double sueldo;
        private String codigo;

        public Empleado() {
            this.nombre = "Empleado"+autonumerico;
            this.edad =(int)(Math.random()*50+20);
            this.sueldo =1000; //Math.random()*1000+1000;
            this.codigo = generadorCodigo();
            //this.codigo = "z"+autonumerico;

        public abstract String generadorCodigo();

        public String getNombre() {
            return nombre;

        public void setNombre(String nombre) {
            this.nombre = nombre;

        public int getEdad() {
            return edad;

        public void setEdad(int edad) {
            this.edad = edad;

        public double getSueldo() {
            return sueldo;

        public void setSueldo(double sueldo) {
            this.sueldo = sueldo;

        public String getCodigo() {
            return codigo;

        public void setCodigo(String codigo) {
            this.codigo = codigo;

        public String toString() {
            return "Empleado [nombre=" + nombre + ", edad=" + edad + ", sueldo=" + sueldo + ", codigo=" + codigo + "]";

        public int compareTo(Empleado o) {//Comparamos por codigo

            return this.codigo.compareToIgnoreCase(o.codigo);   
asked by David Palanco 09.06.2018 в 00:10

1 answer


The documentation recommends (that is, it is not mandatory but it is a good practice) that comparable objects have a certain consistency: it makes no sense that two objects that valen igual (one is not greater than the other), are not considered < em> equals .

I'll give you an example with numbers, so you can see what it means:

Imagine that we have a list of numbers:

List<Integer> numeros= ....

And we want to order it. Since Integer is a class that implements Comparable, numeros.sort() will work without problems.

Now we want to find the repeated numbers. If we have ordered the list, we hope that the repeated numbers are together, so we would not have to check all the other positions for each number:

{ 1, 3, 3, 4, 5, 7}  //si el que está a su lado no es igual, es que no hay repetidos.

To verify that two numbers are equal, it should not matter if we use equals or compareTo:

Integer cinco=5;
Integer otroNum = ...;
boolean b = cinco.equals(otroNum) == (cinco.compareTo(otroNum) == 0);
//Si b no es true SIEMPRE, tenemos una inconsistencia!

And, as an extra bonus, it would be desirable if two objects are considered equal using these two methods, the call to hashCode() in both objects would return the same result, because that way the hash tables would assign them to the same position , helping to find repetitions.

But as I said at the beginning, this is not always necessary: we could find cases where two objects have the same weight but are not the same: if we want to sort a list of people by name alphabetically, we will have an order, but that two people are called equal does not mean they are the same person.

answered by 09.06.2018 / 00:46