Что такое 31? Почему именно это число?
2^5 - 1???
Помогите пожалуйста разобраться!
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 (this == n) return true;
if (first != null ? !first.equals(n.first) : n.first != null) return false;
if (last != null ? !last.equals(n.last) : n.last != null) return false;
return true;
}
@Override
public int hashCode() {
int result = 1;
result = 31 * result + (this.first != null ? this.first.hashCode() : 0);
result = 31 * result + (this.last != null ? this.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")));
}
}