Подскажите на что обратить внимание?
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(Object o) {
// if ((o instanceof Solution)) return true;
// if (this == o) return true;
// if (o == null || getClass() != o.getClass()) return false;
// Solution solution = (Solution) o;
// if (first == null || solution.first == null || last == null || solution.last == null) return false;
// return Objects.equals(first, solution.first) && Objects.equals(last, solution.last);
if (o == this)
return true;
if (!(o instanceof Solution))
return false;
if (o.getClass() != this.getClass()) {
return false;
}
Solution solution = (Solution) o;
return Objects.equals(first, solution.first) && Objects.equals(last, solution.last);
}
public int hashCode() {
return 31 * first.hashCode() + last.hashCode();
}
public static void main(String[] args) {
Set<Solution> s = new HashSet<>();
s.add(new Solution("Donald", "Duck"));
s.add(new Solution("Donald1", "Duck1"));
s.add(new Solution("Donald2", "Duck2"));
System.out.println(s.contains(new Solution("Donald", "Duck")));
for (Solution r : s){
System.out.println(r.hashCode());
}
}
}