Вроде все правильно. Даже на всякий случай попробовал дурной вариант сравнил строки через == всеравно не принимает.
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 == n)
return true;
//if (getClass() != n.getClass())
// return false;
if (!(n instanceof Solution))
return false;
Solution other = (Solution) n;
if (first != null ? !first .equals(other.first) : first != null)
return false;
if (last != null ? !last.equals(other.last) : last != null)
return false;
return true;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = first != null ? first.hashCode() : 0;
result = prime * result + (last != null ? last.hashCode() : 0);
return result;
}
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")));
//s.add(new Solution("dfdf", "dfdf1"));
//System.out.println(s.contains(new Solution("dfdf", "dfdf1")));
}
}