return ((first == other.first || other.first != null && first.equals(other.first))
&& last == other.last || other.last != null && last.equals(other.last));
в 23-24-х строках программы выполняется заявленное условие.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 == this) return true;//Рефлексивность: объект должен равняться себе самому
// if (n == null || !n.getClass().getSimpleName().equals("Solution")) return false;
if(n == null || !(n instanceof Solution)) return false;
Solution other = (Solution) n;
return ((first == other.first || other.first != null && first.equals(other.first))
&& last == other.last || other.last != null && last.equals(other.last));
}
@Override
public int hashCode() {
int hash = 7;
hash = hash + (this.first != null ? this.first.hashCode() : 0);
hash = hash + (this.last != null ? this.last.hashCode() : 0);
return hash;
}
public static void main(String[] args) {
// Solution first = new Solution("first", "last");
// Solution second = new Solution("first", "last");
// System.out.println(first.equals(second));
Set<Solution> s = new HashSet<>();
s.add(new Solution("Donald", "Duck"));
System.out.println(s.contains(new Solution("Donald", "Duck")));
}
}