Отдельная проверка equals выдает true но по условию не проходит, подскажите почему?
package com.javarush.task.task21.task2104;
import java.util.HashSet;
import java.util.Objects;
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 == null || n.getClass() != getClass()) return false;
if (!(n instanceof Solution)) return false;
Solution s = (Solution) n;
if (first != null ? !first.equals(s.first) : s.first != null) return false;
return last != null ? last.equals(s.last) : s.last == null;
}
public int hashCode() {
int result = 1;
return result * 31 * this.first.hashCode() + this.last.hashCode();
}
public static void main(String[] args) {
Set<Solution> s = new HashSet<>();
// Solution s1 = new Solution("Donald", "Duck");
// Solution s2 = new Solution("Donald", "Duck");
// System.out.println(s1.equals(s2));
// System.out.println("s1 hashcode = " + s1.hashCode());
// System.out.println("s2 hashcode = " + s2.hashCode());
s.add(new Solution("Donald", "Duck"));
System.out.println(s.contains(new Solution("Donald", "Duck")));
}
}