Перечитал все комментарии и обсуждения, но так и не понял причины. Прошу подсказать, что не понравилось валидатору.
package com.javarush.task.task21.task2104;
import java.util.HashSet;
import java.util.Set;
/*
Equals and HashCode
*/
public class Solution {
private final String first, last;
public Solution(String first, String last) {
this.first = first;
this.last = last;
}
public boolean equals(Object n) {
if (n == null)
return false;
if (n == this)
return true;
if (!(n instanceof Solution))
return false;
// if (n.getClass() != this.getClass())
// return false;
Solution temp = (Solution)n;
if ((this.first == null && temp.first == null) && (this.last == null && temp.last == null))
return true;
if ((this.first == null && temp.first == null) && temp.last.equals(this.last))
return true;
if (temp.first.equals(this.first) && (this.last == null && temp.last == null))
return true;
else
return temp.first.equals(this.first) && temp.last.equals(this.last);
}
public int hashCode() {
final int PRIME = 31;
int result = 1;
result = PRIME * result + ((first == null) ? 0 : first.hashCode());
result = PRIME * result + ((last == null) ? 0 : last.hashCode());
return result;
}
public static void main(String[] args) {
Set<Solution> s = new HashSet<>();
s.add(new Solution("Cat", "Dog"));
s.add(new Solution("Cat", null));
s.add(new Solution(null, "Dog"));
s.add(new Solution(null, null));
System.out.println(s.contains(new Solution("Cat", "Dog")));
System.out.println(s.contains(new Solution("Cat", null)));
System.out.println(s.contains(new Solution(null, "Dog")));
System.out.println(s.contains(new Solution(null, null)));
}
}