Что же не так с 5м пунктом. Сделал все проверки и на 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;
}
@Override
public boolean equals(Object n) {
if (n == null) return false;
if (this.getClass() != n.getClass())return false;
if(this == n) return true;
if (!(n instanceof Solution)) return false;
if(((Solution)n).first != null && first != null && ((Solution)n).last != null && last != null)
return ((Solution)n).first.equals(first) && ((Solution)n).last.equals(last);
if(((Solution)n).first != null && first != null) return ((Solution)n).first.equals(first);
if(((Solution)n).last != null && last != null) return ((Solution)n).last.equals(last);
return false;
}
@Override
public int hashCode() {
if(first == null && last == null) return 0;
if(first == null) return last .hashCode();
if(last == null) return first.hashCode();
return 31 * first.hashCode() + last.hashCode();
}
public static void main(String[] args) {
Set<Solution> s = new HashSet<>();
s.add(new Solution("Donald", "Duck"));
//Solution solution2 = new Solution(null, null);
System.out.println(s.contains(new Solution("Donald", "Duck")));
//if(!s.contains(solution2)) s.add(solution2);
}
}