Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Implementation of HashTable that allows duplicates by using arraylists

Status
Not open for further replies.

proteome

Technical User
May 14, 2003
115
US
Has anyone seen code that allows duplicates to be added to a hash table. I know in java it is possible to extend a class such as hashtable and add new methods to it. Has anyone implemented a hash that allows duplicates. Such that the add command (if the key is there) add the data as an arraylist.

thanks
 
why not do exactly that?

public class CustomHashTable
{
private HashTable hash = new HashTable();

public void Add(string key, Object obj)
{
ArrayList list = hash[key];

if (list == null)
{
list = new ArrayList();
hash.Add(key, list);
}
list.Add(obj);
}

public ArrayList Find(string key)
{
if (hash.Contains(key))
{
return (ArrayList)hash[key];
}

return new ArrayList();
}
}


This is not tested code. :)
 
I have taken you comments and have adapted them a little, by extended the C# hashTable and adding 2 methods addDuplicates and getDuplicates, basically it adds the items to the hash via ArrayList.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top