Подскажите, где ошибка?
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 (this == n)
return true;
if (n.getClass()!= this.getClass())
return false;
Solution solution = (Solution) n;
if (this.first == null && solution.first == null && this.last == null && solution.last == null)
return true;
if (this.first==null && solution.first!=null || !this.first.equals(solution.first))
return false;
if (this.last == null && solution.last !=null || !this.last.equals(solution.last))
return false;
return true;
}
public int hashCode() {
final int prime = 31;
int result = 1;
if (last == null && first ==null)
return 0;
if (first == null && last!=null)
return result = prime * result + last.hashCode();
if (last == null && first !=null)
return result = prime * result + 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"));
System.out.println(s.contains(new Solution("Donald", "Duck")));
}
}