Что не так?
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;
}
@Override
public boolean equals(Object n) {
if (n == this) {
return true;
}
// if (n.getClass() == null || this.getClass() != n.getClass()) {
// return false;
// }
if (!(n instanceof Solution)) {
return false;
}
Solution sol = (Solution) n;
return first != null && first.equals(sol.first) && last != null && last .equals(sol.last);
// return sol.first != null && sol.last != null && sol.first.equals(first) && sol.last.equals(last);
}
@Override
public int hashCode() {
return 31 * first.hashCode() + last.hashCode();
}
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")));
}
}