Why this two simple objects are not equal?
java
Solution
You have to override the `equals(Object)` method:
Place this in your School class:
@Override
public boolean equals(Object other) {
if (other == this) return true;
if (other == null || !(other instanceof School)) return false;
School school = (School) other;
if (school.id != this.id) return false;
if (!(school.name.equals(this.name))) return false;
if (school.isOpen != this.isOpen) return false;
if (!(school.hashCode().equals(this.hashCode()))) return false;
return true;
}
If you are going to this, it is also wise to override the `hashCode()` method as well.
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (int) id;
result = prime * result + (name != null ? name.hashCode() : 0);
result = prime * result + (isOpen ? 0 : 1);
return result;
}
Additional Information
I believe this is the best explanation of overriding `hashCode()`.
This answer was posted by dmeister for the following post: SO: Hash Code implementation.
I reference this all the time, and it looks like this functionallity is used in Eclipse when generating the `hashCode()` method for a given class.
A for nearly all cases reasonable good implementation was proposed in Josh Bloch's "Effective Java" in item 8. The best thing is to look it up there because the author explains there why the approach is good.
A short version:
Create a int result and assign a non-zero value.
For every field tested in the equals-Method, calculate a hash code c by:
- If the field f is a boolean: calculate `(f ? 0 : 1)`;
- If the field f is a byte, char, short or int: calculate `(int)f`;
- If the field f is a long: calculate `(int)(f ^ (f >>> 32))`;
- If the field f is a float: calculate `Float.floatToIntBits(f)`;
- If the field f is a double: calculate `Double.doubleToLongBits(f)` and handle the return value like every long value;
- If the field f is an object: Use the result of the `hashCode()` method or `0` if `f == null`;
- If the field f is an array: See every field as separate element and calculate the hash value in a recursive fashion and combine the values as described next.
Combine the hash value c with result with:
`result = 37 * result + c`
Return result
This should result in a proper distribution of hash values for most use situations.
Problem
I have a School class: ``` public class School { private String name; private int id; private boolean isOpen; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public boolean isOpen() { return isOpen; } public void setOpen(boolean isOpen) { this.isOpen = isOpen; } } ``` Then I created two instances of School, and compare the equality of the two instances: ``` public static void main(String[] args) { //school1 School school1 = new School(); school1.setId(1); school1.setName("schoolOne"); //school2 School school2 = new School(); school2.setId(1); school2.setName("schoolOne"); //result is false , why? System.out.println("school1 == school2 ? " + school1.equals(school2)); } ``` Even though I set the same `id` and `name` to `school1` & `school2` instances, but `school1.equals(school2)` returns false, why?