4 Replies Latest reply on Jan 15, 2010 1:41 AM by 796440

    HashMap Help

    807580
      Hey,

      Trying to use a hashmap in java and having difficulties. Here is a little example of what i'm trying to do. When using a custom object as a key into the hash map, why does the containsKey always return false below. I'm assuming because Tuple d is a new object. Anybody know how I can get the containsKey to return true?
      Tuple a = new Tuple("a", "b", "c");
      Tuple b = new Tuple("d", "e", "f");
      Tuple c = new Tuple("g", "h", "i");
                
      HashMap<Tuple, Integer> hm = new HashMap<Tuple, Integer>();
      hm.put(a, 1);
      hm.put(b, 2);
      hm.put(c, 3);
                
      Tuple d = new Tuple("d", "e", "f");
           
      if (hm.containsKey(d))
              System.out.println("found it");
        • 1. Re: HashMap Help
          807580
          Anybody know how I can get the containsKey to return true?
          Properly override hashCode() and equals() in the Tuple class.

          ~
          • 2. Re: HashMap Help
            807580
            If all the properties in the class are strings, how should one override hashCode? Something like:
            public int hashCode()
            {
                return a.hashCode() + b.hashCode() + c.hashCode();
            }
            If that would work, in my example, i added the hashcodes..... but, what is the best practice?
            • 3. Re: HashMap Help
              807580
              The only important part is the rule given in the API docs -- equal objects must have equal hashcodes. (But vice-versa isn't a requirement: objects with equal hashcodes do not have to be equal.)

              Theoretically you want to produce hash codes which are evenly distributed.

              But in practice (i.e., not in college) I've never heard of a single case where a simple approach to generating hashcodes wasn't more than sufficient. What you have is probably fine. I wouldn't worry about it unless you profiled your app and found that too much time was spent retrieving things from a hashtable.
              • 4. Re: HashMap Help
                796440
                billru44 wrote:
                If all the properties in the class are strings, how should one override hashCode? Something like:
                public int hashCode()
                {
                return a.hashCode() + b.hashCode() + c.hashCode();
                }
                If that would work, in my example, i added the hashcodes..... but, what is the best practice?
                For toy programs or educational purposes where the goal is to write a hashCode that's correct just as part of undrerstanding how they work and how they're used, that will suffice. But for a real world application where you need it to have a good enough distribution to be fast even when storing a large number of items with similar field values, you'll want something a little more sophisticated. You can find a good general purpose hashCode recipe here:
                [http://java.sun.com/developer/Books/effectivejava/Chapter3.pdf]