0

I have array list in java:

List<Correction> Auv=new ArrayList<>();
List<Correction> Produv=new ArrayList<>();

then I want to substract Produv value by Auv, here's an example:

Produv.add(new Correction("a","b"));
Produv.add(new Correction("a","c"));
Produv.add(new Correction("b","d"));
Produv.add(new Correction("b","c"));

Auv.add(new Correction("c","a"));
Auv.add(new Correction("b","c"));

Produv.removeall(Auv);

but nothing subtracted, the array still contain it initial value, is there any way to do this? I try to override equals(), and still got the same result

here the code of my Correction class:

    public class Correction {
    private String node0;
    private String node1;

    public Correction(String node0, String node1) {
        this.node0 = node0;
        this.node1 = node1;
    }

    public void setNode0(String node0){
        this.node0=node0;
    }
    public void setNode1(String node1){
        this.node1=node1;
    }

    public String getNode0(){
        return node0;
    }
    public String getNode1(){
        return node1;
    }

    @Override
    public boolean equals(Object object){
        boolean same = false;

        if (object != null && object instanceof Correction)
        {
            same = this.node0 == ((Correction) object).node1 && this.node1 == ((Correction) object).node1;
        }

        return same;
    }
}

Solved!! it just simply a mistake on overriding equals() method(thank's guys) here my correction:

   @Override
    public boolean equals(Object object){
        boolean same = false;

        if (object != null && object instanceof Correction)
        {
            same = (this.node0 == ((Correction) object).node1 && this.node1 == ((Correction) object).node0)||(this.node0 == ((Correction) object).node0 && this.node1 == ((Correction) object).node1);
        }

        return same;
    }
bilih
  • 3
  • 2
  • 1
    Should `this.node0` be equal to `object.node1` or `object.node0`? – Sotirios Delimanolis Jan 14 '15 at 19:57
  • 1
    Also, the code you have won't compile. – Sotirios Delimanolis Jan 14 '15 at 19:57
  • 1
    `Auv.add("c","a");` doesn't make any sense – Nathan Hughes Jan 14 '15 at 19:58
  • 1
    Additionally, [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) applies. – rgettman Jan 14 '15 at 19:58
  • 1
    Looks like mostly a simple typo to me. (Besides comparing strings.) However, I'll add that `Correction` overrides `equals` but does not override `hashCode()`, which is poor practice. – markspace Jan 14 '15 at 19:59
  • yea, it's a typo, thanks for noticing, I read that I also have to override hashcode(), but I don't know ho to do it – bilih Jan 14 '15 at 20:05
  • @bilih Overriding hashCode is important if you want to use your Correction class as the key in a HashMap or the element in a HashSet. If you only use it in an ArrayList, it's still a good practice to override hashCode whenever you override equals, since if a.equals(b), a.hashCode() should be equal to b.hashCode(). – Eran Jan 14 '15 at 20:21
  • thank's @Eran, I'll take your suggestion, since I'll use Correction class as element in HashSet further. – bilih Jan 14 '15 at 20:33

1 Answers1

2

Your equals method looks wrong. It makes more sense to compare this.node0 to ((Correction) object).node0.

I think it should be:

public boolean equals(Object object){
    boolean same = false;

    if (object != null && object instanceof Correction)
    {
        same = this.node0.equals(((Correction) object).node0) && this.node1.equals(((Correction) object).node1);
    }

    return same;
}

Also, is this a typo?

Auv.add("c","a");
Auv.add("b","c");

It should probably be :

Auv.add(new Correction ("c","a"));
Auv.add(new Correction ("b","c"));
Eran
  • 387,369
  • 54
  • 702
  • 768