Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java HashSet using customized class as key: "contains()" function always return false

I'm trying to use HashSet, while using my own class "Inner" as key type, as below:

import java.util.HashSet;
class Inner {
    int i;
    String s;
    public Inner(int i, String s) {
        this.i = i;
        this.s = s;
    }
    @Override
    public int hashCode() {
        return super.hashCode();
    }
    @Override
    public boolean equals(Object o) {
        Inner inner = (Inner) o;
        return i == inner.i && s.equals(inner.s);
    }
}

public class testEquals {
    public static void main(String [] args) {
        HashSet<Inner> hi = new HashSet<>();
        hi.add(new Inner(1,"abc"));
        System.out.println(hi.contains(new Inner(1,"abc")));
    }
}

It prints "false"

(1) My question is, as long as I try to use "contains" function, I have to construct a new object from "Inner" class to query, but because it's a new object, hashcode() is different. So I always get a "false" for "contains" functions.

(2) If I change the hashCode() to return "true" like equals when values are the same, then in other scenarios, different objects references are considered as "==" just like one unique reference.

(1) and (2) seems to conflict.

How to solve this?

Thanks!

like image 378
Hind Forsum Avatar asked Feb 01 '26 23:02

Hind Forsum


1 Answers

You should override hashCode in a way that two equal objects will have the same hashCode.

For example:

@Override
public int hashCode() {
    return Objects.hash(i,s);
}

I'm not sure what's your problem with (2). If two objects are equal according to equals(), the HashSet should treat them as identical objects, even if they are not.

If, one the other hand, you wish the HashSet to treat any Inner instance as unique (regardless of the values of its instance variables), simply don't override hashCode and equals. However, it is rarely useful to use HashSet without overriding these methods.

like image 165
Eran Avatar answered Feb 03 '26 12:02

Eran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!