-5

Im devoloping a program that i put some elements into my object within an ArrayList... this is my code

import java.util.ArrayList;

public class ListaConcepto {

public static void main(String[] args) {
    // TODO Auto-generated method stub

ArrayList<CompararListas> Lista = new ArrayList<CompararListas>();

    CompararListas obj1 = new CompararListas("abc", 12.25, "lala", 2);
    CompararListas obj2 = new CompararListas("abc", 13.50, "lala", 3);
    CompararListas obj3 = new CompararListas("poc", 12.50, "jaja", 1);

    Lista.add(obj1);
    Lista.add(obj2);
    Lista.add(obj3);

    }    
}

Then... this is my class with the interface Comparable... and i need a method to implement to see if elements are EQUALS then print the result.

public class CompararListas implements Comparable<CompararListas> {
    private String referencia;
    private double monto;
    private String descripcion;
    private double NumeroParte;


    public CompararListas(String referencia, double monto, String descripcion, double numeroParte) {
        this.referencia = referencia;
        this.monto = monto;
        this.descripcion = descripcion;
        this.NumeroParte = numeroParte;
    }


    public double getMonto() {
        return monto;
    }


    public void setMonto(double monto) {
        this.monto = monto;
    }


    public String getDescripcion() {
        return descripcion;
    }


    public void setDescripcion(String descripcion) {
        this.descripcion = descripcion;
    }


    public double getNumeroParte() {
        return NumeroParte;
    }


    public void setNumeroParte(double numeroParte) {
        NumeroParte = numeroParte;
    }


    public String getReferencia() {
        return referencia;
    }


    public void setReferencia(String referencia) {
        this.referencia = referencia;
    }


    @Override
    public int compareTo(CompararListas o) {
        // TODO Auto-generated method stub
        return 0;
    }

}
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
Antonio Alejos
  • 115
  • 1
  • 6
  • 2
    And the problem is ? you don't know how to implement an equals method ? – azro Oct 21 '18 at 23:28
  • that's right... i was wondering that – Antonio Alejos Oct 21 '18 at 23:31
  • This may be helpful: https://stackoverflow.com/questions/27581/what-issues-should-be-considered-when-overriding-equals-and-hashcode-in-java?rq=1 – Ryan Leach Oct 21 '18 at 23:31
  • First just look on the web, there is dozen of tutos, or use an IDE and click on "implement equals method" and it'll do it for you and you'll see – azro Oct 21 '18 at 23:31
  • CompareTo is basically an equals check, except that you return -1, 0, 1 so that rather then not-equal, and equal as a result, you have 2 not-equal values, 1 and -1, 0 means equal. the reason why there are 2 not-equal values, is it tells the caller whether the object should be before, or after the element being compared. – Ryan Leach Oct 21 '18 at 23:33

2 Answers2

1

Remember Comparable is meant to be used to decide the relationship between to items for the purposes of ordering; it's not quite for equality. In fact, there's no requirement that any two comparable items must be equal.

In your case, you may want to order by NumeroParte, which means "Part Number" and seems like a sensible choice. Every type of data has a different way to be organized.

Your method may look something like

public int compareTo(ComprarListas o){
    return this.NumeroParte - o.getNumeroParte();
}

Which works because both are integer numbers.

Or the appropriate property of your object - you may want to order based on price instead for instance.

iajrz
  • 749
  • 7
  • 16
  • This is a matter of what you think of the problem; there's thinking to be done. The goal will tell you what method you should use; part number may be enough. – iajrz Oct 21 '18 at 23:48
  • 1
    I only need to see if the elements of my objects are equals among them. i'm looking to HashCode and equals method – Antonio Alejos Oct 21 '18 at 23:52
  • That may help; in that case, you don't need to use comparable - comparable is for ordering. If you just need "equals or not" (boolean), then just use `equals`. I think it'd be something like comparing all of the properties in the form ` return this.a == o.a && this.b.equals(o.b) && ...` – iajrz Oct 21 '18 at 23:54
0

If you want to know just if an object is equal to another you have to implemet the equals method (which doesn't requiere you to declare any interface, every class can do it)

@Override
public boolean equals(Object o) {
    CompararListas other = (CompararListas) o;
    return ...  
}

with this method you can return what makes the two objects equals to you, if you just have to look for referencia.equals(other.referencia) or if you have to compare every property like referencia.equals(other.referencia) && monto == other.monto && ...

But if you want to compare elements in order to do something like ordering them there you have to implement the Comparable interface and implement the compareTo method

@Override
public int compareTo(CompararListas o) {
    if (NumeroParte < o.NumeroParte)
        return -1;
    else if (NumeroParte > o.NumeroParte)
        return 1;
    return 0;
}

This will make the objects able to compare each other and for example know if one is "smaller" than other according to your criteria (in this example I only used NumeroParte and made it explicit to be easy to understand, but the key is that you can use any criteria you want to compare the objects and if you want order them later).

These are solutions to different problems, you have to identify which one is the better in your case.

amoto
  • 16
  • 3