简体   繁体   English

从java列表中删除相同的项目

[英]Remove equal item from java list

I have a list of items, where each item is a simple class containing 2 public strings. 我有一个项目列表,其中每个项目都是一个包含2个公共字符串的简单类。 I have an equals method that simply makes use of the equalsIgnoreCase methot of String for both strings. 我有一个equals方法,只使用两个字符串的equalsIgnoreCase methot。

public class data
{
    public String a;
    public String b;

    public boolean equals(data d)
    {
        if(a.equalsIgnoreCase(d.a) && b.equalsIgnoreCase(d.b))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

I want to be able to remove an element even if it's not the same instance of the one in the list but equal to it. 我希望能够删除一个元素,即使它与列表中的元素不同,但是等于它。

Right now I'm doing this: 现在我这样做:

public void remove(data dataToRemove)
{
    for(data i : _list)
    {
        if(i.equals(dataToRemove))
        {
            _list.remove(i);
            break;
        }
    }
}

Is there a better way to do this? 有一个更好的方法吗?

A few comments: 一些评论:

  • Your equals method does not override the equals method of Object (argument should be of Object type, not data type). 你的equals方法不会覆盖Objectequals方法(参数应该是Object类型,而不是data类型)。
  • You should improve your equals method to account for nulls etc. 你应该改进你的equals方法来计算空值等。
  • And finally, you should override hashcode() too when you override equals() - if not you might encounter some weird behaviors when using Sets or Maps for example. 最后,当你重写equals()时,你也应该覆盖hashcode() - 如果不是,你可能会在使用Sets或Maps时遇到一些奇怪的行为。

If you properly override the equals method, you can then just use the remove method. 如果正确覆盖equals方法,则可以使用remove方法。 See below the auto generated equals and hashcode generated by Netbeans, amended to use the equalsIgnoreCase method. 请参阅下面的Netbeans生成的自动生成的equalshashcode ,修改为使用equalsIgnoreCase方法。

public static void main(String[] args) {
    List<Data> list = new ArrayList<Data>();
    list.add(new Data("a", "b"));
    list.add(new Data("a", "c"));
    System.out.println(list.size()); //2
    list.remove(new Data("A", "b"));
    System.out.println(list.size()); //1
}

public static class Data {

    public String a;
    public String b;

    public Data(String a, String b) {
        this.a = a;
        this.b = b;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) return false;
        if (getClass() != obj.getClass()) return false;
        final Data other = (Data) obj;
        boolean sameA = (this.a == other.a) || (this.a != null && this.a.equalsIgnoreCase(other.a));
        if (!sameA) return false;
        boolean sameB = (this.b == other.b) || (this.b != null && this.b.equalsIgnoreCase(other.b));
        if (!sameB) return false;
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 89 * hash + (this.a == null ? 0 :this.a.toUpperCase().hashCode());
        hash = 89 * hash + (this.b == null ? 0 : this.b.toUpperCase().hashCode());
        return hash;
    }

}

The clearest way is to just call the Remove method of the list without any loops and pass in your object the parameter. 最明确的方法是在没有任何循环的情况下调用列表的Remove方法,并在对象中传入参数。 It uses the equals method that you defined on the object to find and remove it if it exists in the list. 它使用您在对象上定义的equals方法来查找并删除它(如果它存在于列表中)。

_list.remove(data);

You also don't have to specify the hashcode method, but you should get in the habit of always creating one when you override the equals method in case you use it in a collection like a Set or a Map. 您也不必指定哈希码方法,但是当您在集合或地图等集合中使用它时,您应该养成在重写equals方法时始终创建一个方法的习惯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM