0

I've found the following code here. It is argued that "The programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables." Another questions are why 7 and 31 are selected on line 20, 21 and 22?

1.  public class Test
2.  {
3.      private int num;
4.      private String data;
5.
6.      public boolean equals(Object obj)
7.      {
8.          if(this == obj)
9.              return true;
10.         if((obj == null) || (obj.getClass() != this.getClass()))
11.             return false;
12.         // object must be Test at this point
13.         Test test = (Test)obj;
14.         return num == test.num &&
15.         (data == test.data || (data != null && data.equals(test.data)));
16.     }
17.
18.     public int hashCode()
19.     {
20.         int hash = 7;
21.         hash = 31 * hash + num;
22.         hash = 31 * hash + (null == data ? 0 : data.hashCode());
23.         return hash;
24.     }
25.
26.     // other methods
27. }
Jack
  • 6,430
  • 27
  • 80
  • 151
  • @user2864740 that is irrelevant, the title of question is "Does MySQL index foreign key columns automatically?" – Jack Oct 14 '14 at 02:26
  • Also: http://stackoverflow.com/questions/11468589/hashcode-bucket-distribution-in-java/11468648#11468648 , http://stackoverflow.com/questions/1757363/java-hashmap-performance-optimization-alternative – user2864740 Oct 14 '14 at 02:29
  • 1
    And, in a shameless plug, http://powerfield-software.com/?p=615 :-) – paxdiablo Oct 14 '14 at 02:30
  • See: http://en.wikipedia.org/wiki/Hash_table – user2864740 Oct 14 '14 at 04:49

0 Answers0