"Thinkinginjava" 51, hash and hash code

Source: Internet
Author: User

/*** Book: Thinking in Java * Features: Hashing and hashing * file: groundhog.java* time: May 3, 2015 09:42:54* Author: cutter_point*/package Lesson17containers;public class groundhog{protected int number;//protection Type, inherited or protected type public groundhog (int n) {number = n;} Public String toString () {return ' Groundhog # ' + number;}}


/*** Book: Thinking in Java * Features: Hashing and hashing * 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;p rivate Boolean shadow = Rand.nextdouble () > 0.5;//determines the range of values produced, nextdouble produces a value between 0 and 1 public String toString () {if (shadow) Return "Six more weeks of winter!"; Elsereturn "Early spring!";}}


/*** Book: Thinking in Java * Features: Hash and hash codes, a weather forecast system that links Groundhog and Prediction objects * 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.*;p ublic class Springdeterctor{public static <t extends groundhog> void detectspring (class<t> type) throws exception{constructor<t> Ghog = Type.getconstructor ( Int.class);//Gets the constructor for the type in which the parameter is int, this is the reflection Map<groundhog, prediction> map = new Hashmap<groundhog, prediction> ( for (int i = 0; i < ++i) {//Initialize the values inside the map, the second parameter is the two randomly generated conditions map.put (ghog.newinstance (i), new Prediction ());} Print ("map =" + map); Groundhog GH = ghog.newinstance (3);p rint ("Looking up prediction for" + GH); if (Map.containskey (GH))//etc We'll find we can't find the number 3rd Prin    T (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!, Groundh OG #7 =early spring!, groundhog #8 =six more weeks of winter!, Groundhog #3 =early spring!, groundhog #1 =six more weeks of Wi  nter!, 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





/*
* Cannot find reason No. 3rd:
* The problem occurs when groundhog automatically inherits the base class object, so here we use the Hashcode method of object to generate the hash code, and his default is to use the object's address to calculate the hash code,
* Therefore, the hash code of the first instance generated by Groundhog (3) and by groundhog GH = ghog.newinstance (3); the second instance hash code generated is different
* And we're looking for it by the latter
*
Solution
* Overwriting methods that only write Hashcode methods will not work unless you overwrite equals at the same time
*/















"Thinkinginjava" 51, hash and hash code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.