I want to add custom type objects to Set. I have several identical values, i.e. They have the same values ββfor their public variables.
I do not need to add more than one instance of the same object to the set, but every time a new object is created, it is always added.
This is because the equals method for the Object class implements the most different possible equivalence relation for objects: "For any non-empty reference values ββx and y, this method returns true if and only if x and y refer to the same object ( x == y is true).
Is it possible to override the equals method for this object to define it differently?
Thanks everyone for resolving the issue.
Continuity for Java objects is determined by overriding the Java equals () method.
@Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((uri == null) ? 0 : uri.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (!(obj instanceof Resource)) return false; Resource other = (Resource) obj; if (uri == null) { if (other.uri != null) return false; } else if (!uri.equals(other.uri)) return false; return true; }
Ankur source share