Results 1 to 8 of 8
Thread: Problem with Hashtable key.
- 09-08-2010, 01:36 PM #1
Member
- Join Date
- Aug 2010
- Posts
- 35
- Rep Power
- 0
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:
And an example of the HashIndex in use: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>(); }
I would expect the output from this to be: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")));
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:
- 09-08-2010, 01:56 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
You need to override the hashcode and equals methods in your HashIndex class.
- 09-08-2010, 02:29 PM #3
Member
- Join Date
- Aug 2010
- Posts
- 35
- Rep Power
- 0
Hmmmm ....
Ok, I've put a method that looks like this into the HashIndex class, just to test it:
I think that should make the hashtable think that all HashIndexes are equal.Java Code:public int hashCode() { return 1; }
But ... I still get the same result as before!Last edited by XmisterIS; 09-08-2010 at 02:38 PM.
- 09-08-2010, 03:27 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
You didn't override anything, you created an overloaded equals( ... ) method that never is called. The signature of the equals( ... ) method has to be:
kind regards,Java Code:public boolean equals(Object that) { ... }
Jos
- 09-08-2010, 04:13 PM #5
Member
- Join Date
- Aug 2010
- Posts
- 35
- Rep Power
- 0
Ah! Now it works. Many thanks!
- 09-11-2010, 01:49 AM #6
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Isn't HashTable legacy code?
- 09-11-2010, 02:39 AM #7
- 09-11-2010, 08:19 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
hashtable
By vijayabaskar in forum Java ServletReplies: 0Last Post: 04-06-2009, 08:20 AM -
hashtable
By vijayabaskar in forum Advanced JavaReplies: 2Last Post: 04-06-2009, 08:05 AM -
Jtable duplicates through Hashtable (JTable condition problem) my assignment plz help
By salmanpirzada1 in forum Advanced JavaReplies: 2Last Post: 05-15-2008, 10:15 AM -
Hashtable example
By Java Tip in forum Java TipReplies: 0Last Post: 02-15-2008, 08:43 AM -
HashTable problem
By jhetfield18 in forum New To JavaReplies: 2Last Post: 12-16-2007, 10:27 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks