如何在 java 中使用 removeall() 减去 ArrayList?

how to subtract ArrayList using removeall() in java?

我在 java 中有数组列表:

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

然后我想用Auv减去Produv值,这里有一个例子:

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);

但没有减去任何东西,数组仍然包含它的初始值,有什么办法可以做到这一点? 我尝试覆盖 equals(),但仍然得到相同的结果

这里是我更正的代码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;
    }
}

解决了!! 重写 equals() 方法只是一个错误(谢谢大家) 这里是我的更正:

   @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;
    }

你的 equals 方法看起来不对。比较 this.node0((Correction) object).node0 更有意义。

我觉得应该是:

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;
}

还有,这是打错了吗?

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

大概应该是:

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