简体   繁体   English

使用对象删除HashSet

[英]Removing HashSet with Object

When I try to remove a HashSet that takes an object, the hashset.contains(Object) and iterator.equals(Object) are always false, even when they should be true. 当我尝试删除带有对象的HashSet ,即使hashset.contains(Object)iterator.equals(Object)应该为true,也始终为false。

public boolean removeElement(Element element)
{        
    Iterator<Element> itr = elements.iterator();
    while (itr.hasNext()) {
        Element oldElement = itr.next();
        if (itr.equals(new Element(element.eString, element.eInt, element.eBoolean))) {
            itr.remove();
            return true;
        }
        if (elements.contains(new Element(element.eString, element.eInt, element.eBoolean))) {
            elements.remove(new Element(element.eString, element.eInt, element.eBoolean));
            return true;
        }
    }
    return false;
}

Is this a feature of Java, a bug, or am I simply coding this wrong? 这是Java的功能,错误还是我只是在编写错误的代码? This seems like the logical solution to removing, but it always fails without throwing any errors. 这似乎是删除的逻辑解决方案,但它始终会失败而不会引发任何错误。

itr.equals(new Element(element.eString, element.eInt, element.eBoolean))

This will always return false because you're comparing an Iterator to an Element which are completely different types of objects. 这将始终返回false,因为您正在将IteratorElement完全不同的对象进行比较。 You want to compare the element to itr.next() which you have saved previously to a local variable. 您想将元素与先前保存到本地变量的itr.next()进行比较。

if (elements.contains(new Element(element.eString, element.eInt, element.eBoolean))) {

This would return false as well if you didn't override the equals() method in the class Element . 如果您没有重写Element类中的equals()方法,则此方法也会返回false。 The default Object.equals() method is used which dictates that the two references should refer to the same object in order to be equal. 使用默认的Object.equals()方法,该方法指示两个引用应引用同一对象才能相等。 In this case, you're comparing against a new object that you create using new Element(element.eString, element.eInt, element.eBoolean) . 在这种情况下,您要与使用new Element(element.eString, element.eInt, element.eBoolean)创建的新对象进行比较。 To solve this you need to override the equals method to specify how the objects of type Element must be checked for equality. 为了解决这个问题,您需要重写equals方法,以指定必须如何检查Element类型的对象是否相等。

For example, if Element has the following fields: 例如,如果Element具有以下字段:

String eString;
int eInt;
boolean eBoolean;

Then you can override equals as follows: 然后,您可以按以下方式覆盖equals

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Element other = (Element) obj;
    if (eBoolean != other.eBoolean)
        return false;
    if (eInt != other.eInt)
        return false;
    if (eString == null) {
        if (other.eString != null)
            return false;
    } else if (!eString.equals(other.eString))
        return false;
    return true;
}

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

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