[ThinkingInJava] 51. Hash and hash code, thinkinginjava51
/*** Book: Thinking In Java * function: hash and hash code * file: Groundhog. java * Time: May 3, 2015 09:42:54 * Author: cutter_point */package Lesson17Containers; public class Groundhog {protected int number; // protection type. After inheritance, the protection type is still public Groundhog (int n) {number = n ;}public String toString () {return "Groundhog #" + number ;}}
/*** Book: Thinking In Java * function: hash and hash code * file: Prediction. java * Time: May 3, 2015 09:42:54 * Author: cutter_point */package Lesson17Containers; import java. util. random; public class Prediction {private static Random rand = new Random (47); private boolean shadow = rand. nextDouble ()> 0.5; // determines the range of the generated values. The value produced by nextDouble is public String toString () {if (shadow) between 0 and 1) return "Six more weeks of Winter! "; Elsereturn" Early Spring! ";}}
/*** Book: Thinking In Java * function: hashes and hashes. A Weather Forecast System associates Groundhog with a Prediction object. * file: SpringDeterctor. java * Time: May 3, 2015 09:42:54 * Author: cutter_point */package Lesson17Containers; import java. lang. reflect. constructor; import java. util. hashMap; import java. util. map; import static net. mindview. util. print. *; public class SpringDeterctor {public static <T extends Groundhog> void detectSpring (Class <T> type) throws Exception {Constructor <T> ghog = type. getConstructor (int. class); // get the Int constructor as the parameter in type. This is the reflection Map <Groundhog, Prediction> map = new HashMap <Groundhog, Prediction> (); for (int I = 0; I <10; ++ I) {// initialize the values in the map. The second parameter is a randomly generated map. put (ghog. newInstance (I), new Prediction ();} print ("map =" + map); Groundhog gh = ghog. newInstance (3); print ("Looking up prediction for" + gh); if (map. containsKey (gh) // we will find that no 3 print (map. get (gh); else print ("Key not found:" + gh);} public static void main (String [] args) throws Exception {detectSpring (Groundhog. class );}}
Output:
Map = {Groundhog #9 = Six more weeks of Winter !, Groundhog #4 = Six more weeks of Winter !, Groundhog #2 = Early Spring !, Groundhog #7 = Early Spring !, Groundhog #8 = Six more weeks of Winter !, Groundhog #3 = Early Spring !, Groundhog #1 = Six more weeks of Winter !, Groundhog #6 = Early Spring !, Groundhog #5 = Early Spring !, Groundhog #0 = Six more weeks of Winter !} Obj1
Looking up prediction for Groundhog #3 obj1
Key not found: Groundhog #3 obj1
/*
* Cause 3 cannot be found:
* The problem occurs when Groundhog automatically inherits the base class Object. Therefore, the hashCode method of the Object is used to generate a hash code. The default hash code is calculated using the Object address,
* Therefore, the hash code of the First Instance generated by Groundhog (3) is different from that of the second instance generated by Groundhog gh = ghog. newInstance (3 ).
* We search by the latter.
*
* Solution:
* The override method of the hashCode method cannot be run unless you overwrite equals at the same time.
*/