将两个对象与哈希集进行比较的适当方法是什么?

发布于 2024-12-03 01:12:29 字数 623 浏览 3 评论 0 原文

我有一个人类,其中有一个名字和 HashSet 中的朋友列表。

我想重写此 Person 类的 equals 方法。以下是我到目前为止所写的内容。

但我对此不确定,因为我知道 HashSet 不一定必须按顺序,而且我还听说我必须重写 HashCode 方法。

我应该在下面的代码中进行哪些修改才能正确实现 equals 方法?

public boolean equals(Note target){
    if(this.name==target.getName() && this.friends == target.getFriends()){
        return true;
    }
    return false;
}

public HashSet<Person> getFriends(){
    return this.friends;
}

编辑

//override hashCode()
public int hashCode() {
    return name.hashCode() + friends.hashCode();
}

I have a person class which has a name and a list of friends in HashSet.

I want to override an equals method for this Person class. Below is what I have written so far.

But I am unsure about this because, I know that HashSet does not necessarily have to be in order, and I also heard that I have to override HashCode method.

What modifications should I make in below codes to correctly implement equals method?

public boolean equals(Note target){
    if(this.name==target.getName() && this.friends == target.getFriends()){
        return true;
    }
    return false;
}

public HashSet<Person> getFriends(){
    return this.friends;
}

edit

//override hashCode()
public int hashCode() {
    return name.hashCode() + friends.hashCode();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

任性一次 2024-12-10 01:12:29

== 运算符对于 HashSetname 来说肯定是错误的。如果你想在java中比较对象,请使用equals方法。 == 运算符比较 JVM 管理的内部对象 ID。 HashSetequals 方法的约定位于 AbstractSet.equals()

@Override
public boolean equals(Object o){

    if (o instanceof Note) {
        Note target = (Note) o;
        if(this.name.equals(target.getName()) && this.friends.equals(target.getFriends())){
            return true;
        }
    }
    return false;
}

您可能还想检查 null,也许 namefiends 可以为 null。

关于equalshashCode已经写了很多。最好的方法是查看 JavaDoc 中的 lava.lang.Object.equals()

The == operator is definitely wrong for the HashSet and name. If you want to compare objects in java use the equals method. The == operator compares the internal object IDs managed by the JVM. The contract for HashSet's equals method is document in AbstractSet.equals().

@Override
public boolean equals(Object o){

    if (o instanceof Note) {
        Note target = (Note) o;
        if(this.name.equals(target.getName()) && this.friends.equals(target.getFriends())){
            return true;
        }
    }
    return false;
}

You may want to check for null's as well, maybe name and fiends can be null.

A lot has been writen regarding equals and hashCode. The best way is to look into the JavaDoc for lava.lang.Object.equals().

三生殊途 2024-12-10 01:12:29

按照您目前的 equals(Note) 方法设置方式,它(几乎)永远不会返回 true。使用 String 对象时,应始终使用 equals(String),而不是 ==

我将创建如下所示的 Note.equals(Note) 方法:

@Override
public boolean equals(Object obj){
    if(obj instanceof Note) {
        Note target = (Note) obj;
        if(name.equals(target.name) && friends.containsAll(target.friends)
              && friends.size() == target.friends.size()){
            return true;
        }
    }
    return false;
}

您会注意到我上面提供的 equals(Note) 方法也不执行 Friends.equals(target.getFriends())。这是因为您正在比较包含的 HashSet,而不是 HashSet 的内容。

最后,如果您希望您的 Note 类正确散列,您还需要重写您的 hashCode() 方法。您可以在 hashCode() 方法.html” rel="nofollow">对象文档。

public int hashCode() {
    return name.hashCode() + friends.hashCode();
}

The way you currently have your equals(Note) method setup, it will (almost) never return true. When working with String objects you should always use equals(String), never ==.

I would create my Note.equals(Note) method something like this:

@Override
public boolean equals(Object obj){
    if(obj instanceof Note) {
        Note target = (Note) obj;
        if(name.equals(target.name) && friends.containsAll(target.friends)
              && friends.size() == target.friends.size()){
            return true;
        }
    }
    return false;
}

You will notice that the equals(Note) method I provided above also doesn't do friends.equals(target.getFriends()). This is because you are comparing the containing HashSet, and not the contents of the HashSet.

Finally, if you want your Note class to hash properly, you will also need to override your hashCode() method. You can read more about this contract in the description of the hashCode() method in the Object documentation.

public int hashCode() {
    return name.hashCode() + friends.hashCode();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文