Results 1 to 8 of 8
  1. #1
    XmisterIS is offline Member
    Join Date
    Aug 2010
    Posts
    35
    Rep Power
    0

    Default Problem with Hashtable key.

    Hi all,

    I need a hashtable with multi-dimensional keys (e.g. 3 keys per value).

    My test harness looks like this:

    My HashIndex class for 1 key-, 2 keys- and 3 keys-per-value indexing:
    Java Code:
    public class HashIndex extends Object
    {
      public HashIndex(Object key1) {
        m_keys.add(key1);
      }
    
      public HashIndex(Object key1, Object key2) {
        m_keys.add(key1);
        m_keys.add(key2);
      }
    
      public HashIndex(Object key1, Object key2, Object key3) {
        m_keys.add(key1);
        m_keys.add(key2);
        m_keys.add(key3);
      }
    
      public boolean equals(HashIndex idx)
      {
      	System.out.println("got here");
      	return m_keys.equals(idx.m_keys);
      }
    
      protected Vector<Object> m_keys = new Vector<Object>();
    }
    And an example of the HashIndex in use:
    Java Code:
    Hashtable<HashIndex, String> ht = new Hashtable<HashIndex, String>();
    ht.put(new HashIndex("a", "b", "c"), new String("The quick brown fox jumps over the lazy dog."));
    System.out.println(ht.get(new HashIndex("a", "b", "c")));
    I would expect the output from this to be:
    got here
    true


    But what I am actually getting is just:
    null

    Therefore, the Hashtable is not using the HashIndex's equals method.

    Any ideas? :confused::confused::confused:

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    You need to override the hashcode and equals methods in your HashIndex class.

  3. #3
    XmisterIS is offline Member
    Join Date
    Aug 2010
    Posts
    35
    Rep Power
    0

    Default

    Hmmmm ....

    Ok, I've put a method that looks like this into the HashIndex class, just to test it:

    Java Code:
    public int hashCode()
    {
      return 1;
    }
    I think that should make the hashtable think that all HashIndexes are equal.

    But ... I still get the same result as before!
    Last edited by XmisterIS; 09-08-2010 at 03:38 PM.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    You didn't override anything, you created an overloaded equals( ... ) method that never is called. The signature of the equals( ... ) method has to be:

    Java Code:
    public boolean equals(Object that) { ... }
    kind regards,

    Jos

  5. #5
    XmisterIS is offline Member
    Join Date
    Aug 2010
    Posts
    35
    Rep Power
    0

    Default

    Ah! Now it works. Many thanks!

  6. #6
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Isn't HashTable legacy code?

  7. #7
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Quote Originally Posted by al_Marshy_1981 View Post
    Isn't HashTable legacy code?
    Doesn't appear to be.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by al_Marshy_1981 View Post
    Isn't HashTable legacy code?
    Nope, a Hashtable (lowercase tee) is to a HashMap as a Vector is to an ArrayList; the older classes have synchronized methods and are retrofitted to match the Collections framework. StringBuffer and StringBuilder have a similar relationship.

    kind regards,

    Jos

Similar Threads

  1. hashtable
    By vijayabaskar in forum Java Servlet
    Replies: 0
    Last Post: 04-06-2009, 09:20 AM
  2. hashtable
    By vijayabaskar in forum Advanced Java
    Replies: 2
    Last Post: 04-06-2009, 09:05 AM
  3. Replies: 2
    Last Post: 05-15-2008, 11:15 AM
  4. Hashtable example
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-15-2008, 09:43 AM
  5. HashTable problem
    By jhetfield18 in forum New To Java
    Replies: 2
    Last Post: 12-16-2007, 11:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •