На локальной машине проверил разные варианты с null значениями.
Спасибо.
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(Solution n) {
if (this == n) {
return true;
}
if (n instanceof Solution) {
//if (this.hashCode()==n.hashCode()) {
if (n.first != null && n.last != null && this.first != null && this.last != null) {
return this.first.equals(n.first) && this.last.equals(n.last);
} else if (this.first==null && this.last==null && n.first==null && n.last==null) {
return true;
} else if (this.first == null && n.first == null ) {
return this.last.equals(n.last);
} else if (this.last == null && n.last == null ) {
return this.first.equals(n.first);
}
//}
}
return false;
}
public int hashCode() {
int fCode = first==null ? 1 : first.hashCode();
int sCode = last==null ? 1 : last.hashCode();
return 31 * fCode + sCode;
}
public static void main(String[] args) {
Set<Solution> s = new HashSet<>();
s.add(new Solution("Donald", "Duck"));
System.out.println(s.contains(new Solution("Donald", "Duck")));
}
}