public class object
{
public static void main(String[] args)
{
String s=new String("Harry");
int hash=0;
for(int i=0;i<s.length();i++)
{
hash=31*hash+s.charAt(i);
}
System.out.println(hash);
}
}
使用hashCode的两种方法
public int hashCode2()
{
return 7*name.hashCode()+11*new Double(salary).hashCode()+13*hireDay.hashCode();
}
或者:
public int hashCode3()
{
return 7*Objects.hashCode(name)+13*Objects.hashCode(hireDay)+11*Double.hashCode(salary);
}
run结果相同