Уже трижды код переписывал с нуля. Все проверки, которые я ему придумал, проходят, НО s.contains возвращает false. Как это можно проверить? Почему не работает?
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(Solution n) {
if(n==this) return true;
if(n==null) return false;
if(n.getClass() != this.getClass()) return false;
/*
if((n.first==null && this.first!=null) || (n.first!=null && this.first==null)) return false;
if((n.last==null && this.last!=null) || (n.last!=null && this.last==null)) return false;
boolean firstBool = (n.first==null && this.first==null) || (n.first.equals(this.first));
boolean lastBool = (n.last==null && this.last==null) || (n.last.equals(this.last));
return firstBool && lastBool;
*/
return (first == n.first
|| (first != null &&first.equals(n.first)))
&& (last == n.last
|| (last != null && last.equals(n.last)));
}
public int hashCode() {
int firstHashCode = (first == null) ? 0 : first.hashCode();
int lastHashCode = (last == null) ? 0 : last.hashCode();
return 31*firstHashCode + lastHashCode;
}
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")));
System.out.println("t" + new Solution("Donald", "Duck").equals(new Solution("Donald", "Duck")));
System.out.println("f" + new Solution("Dd", "Duck").equals(new Solution("Donald", "Duck")));
System.out.println("t" + new Solution(null, "Duck").equals(new Solution(null, "Duck")));
System.out.println("t" + new Solution(null, null).equals(new Solution(null, null)));
System.out.println("t" + new Solution("Donald", null).equals(new Solution("Donald", null)));
System.out.println("f" + new Solution("Donald", "d").equals(new Solution("Donald", null)));
}
}