java – Error using Collections.sort()

Question:

Studying collections, I tried to use the following example in Eclipse:

public class TestaLista {

    public static void main(String[] args) {
        List <Conta> list = new ArrayList<Conta>();

        Conta cc1 = new ContaCorrente();
        cc1.setNumero(10);

        Conta cc2 = new ContaCorrente();
        cc2.setNumero(5);

        Conta cc3 = new ContaCorrente();
        cc3.setNumero(2);


        list.add(cc1);
        list.add(cc2);
        list.add(cc3);

        System.out.println(list);
        Collections.sort(list);
        System.out.println("------------");
        System.out.println(list);

    }

}

My ContaCorrente class is a child of Account and implements the Comparable class. However, when using the sort() method of Collections , I get the error message:

The method sort(List) in the type Collections is not applicable for the arguments (List).

I cannot find the problem in the code. Someone help?


Account Class:

public abstract class Conta  {
    protected double saldo;
    protected int numero;
    private int agencia;
    private String nome;

    public double getSaldo() {
        return saldo;
    }

    public void deposita(double valor) {
        if (valor > 0) {
            this.saldo += valor;
        } else {
            throw new ValorInvalidoException(valor);
        }
        System.out.println("Fim do deposita");
    }

    public void saca(double valor) {
        this.saldo -= valor;
    }

    public abstract void atualiza(double taxaSelic);

    public int getNumero() {
        return numero;
    }

    public void setNumero(int numero) {
        this.numero = numero;
    }

    public int getAgencia() {
        return agencia;
    }

    public void setAgencia(int agencia) {
        this.agencia = agencia;
    }
    public String getNome() {
        return nome;
    }
    public void setNome(String nome) {
        this.nome = nome;
    }
    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof Conta)){
            return false;
        }
        Conta c = (Conta)obj;
        return this.numero == c.numero || this.nome.equals(c.nome);
    }
    @Override
    public String toString() {

        return "Conta de número " + this.numero;
    }

}

Answer:

For the object to be sorted in the list, 2 things are needed:

  • The object implements Comparable<X> , where X is the type of objects to be compared (in your case, Conta ).
  • The object overrides the compareTo() function of Comparable .

To override Comparable compareTo() method, you have to implement a way to evaluate when one object is larger or smaller than another. For example:

@Override 
public int compareTo(Conta conta) {
  if (conta.saldo == this.saldo) return 0;
  if (conta.saldo < this.saldo) return 1;
  /*if (conta.saldo > this.saldo)*/ return -1;
}

Note that the method must return 0 if the objects are equal , 1 when the this object is greater than the parameter sent object, and -1 when the this object is less than the parameter sent object.

Scroll to Top