Подскажите, что не так в коде? Вывод вроде верный, и в Set все верно добавляется
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 obj) {
if (obj == this){return true;}
else if (obj.getClass() == this.getClass()){
Solution n = (Solution) obj;
if (first != null && n.first != null && last != null && n.last != null) {
return n.first.equals(first) && n.last.equals(last);
}
else if (first == null && n.first == null && last != null && n.last != null){
return n.last.equals(last);
}
else if (first != null && n.first != null && last == null && n.last == null) {
return n.first.equals(first);
}
else return false;
}
else return false;
}
public int hashCode() {
int result = first != null ? first.hashCode() : 0;
result = 31 * result + (last != null ? last.hashCode() : 0);
return result;
}
public static void main(String[] args) {
Set<Solution> s = new HashSet<>();
s.add(new Solution(null, "Duck"));
System.out.println(s.contains(new Solution(null, "Duck")));
}
}