Click here to Skip to main content
15,912,756 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi

I have used hashtable for the following scenario.

100 Sam
100 John

Since hash table key is unqiue we cannot add 100 again . So i used a list as follows .

C#
List<string> List = new List<string>();
list.add(sam)
list.add(John)

hashtable ht = new Hashtable()
ht.add(100,list) 

Now how do I iterate & display the values in output as shown?

100 Sam
John

Thanks in advance
Posted
Updated 9-Feb-12 14:52pm
v2
Comments
Sergey Alexandrovich Kryukov 9-Feb-12 20:53pm    
Fixed few typo and code formatting (but not code bugs).
--SA

You should never use Hashtable or any other non-generic collections in new development. They are superseded by a set of generic types as early as of .NET Framework v.2.0, when generics were introduced. Non-generic non-specialized collections are bad due to a need for type casts.

Choose from System.Collection.Generic.Dictionary, System.Collection.Generic.SortedDictionary or System.Collection.Generic.SortedList; they differ mostly in different trade-off between redundancy and performance.

Please see http://msdn.microsoft.com/en-us/library/system.collections.generic.aspx[^].

You problem will look like this:
C#
using IntIndexedStringDictionary = Dictionary<int, string>;
using Pair = KeyValuePair<int, string>;

//...

IntIndexedStringDictionary dictionary = new IntIndexedStringDictionary();
dictionary.Add(1, "first");
dictionary.Add(3, "third");
//...

foreach (Pair pair in dictionary) {
    int key = pair.Key;
    string value = pair.Value;
    //...
}


—SA
 
Share this answer
 
v5
Comments
ProEnggSoft 9-Feb-12 21:22pm    
Using the Generic Dictionary class is the best solution in my view too, so I voted 5.
Sergey Alexandrovich Kryukov 9-Feb-12 21:26pm    
Thank you,
--SA
Tanzy123 9-Feb-12 23:43pm    
helpfull ans.. +5
Sergey Alexandrovich Kryukov 10-Feb-12 0:07am    
Thank you,
--SA
You could do it like this;

C#
IList<string> list = new List<string>();
list.Add("Sam");
list.Add("John");

Hashtable ht = new Hashtable();
ht.Add(100, list);

foreach (object key in ht.Keys)
{
    Console.Write("{0} ", key);
    foreach (string name in (IList<string>)ht[key])
    {
        Console.WriteLine(name);
    }
}


Although I recommend using IDictionary and Dictionary from System.Collection.Generic instead of hashtable.

Hope this helps,
Fredrik
 
Share this answer
 
Comments
santhosh-padamatinti 9-Feb-12 7:47am    
Usually i uses List and dictionary only. Could you please tell me when exactly we have to use Hash table.
Fredrik Bornander 9-Feb-12 8:05am    
I think the internal implementation of Dictionary is a hash table. The Dictionary is a generic class that gives you type safety when you want it and allows you to ignore it when you dont (new Dictionary<object, object>()), also because of no boxing unboxing it should be faster as well. But for me, the typesafety argument coupled with the fact that you don't have to cast all the time makes Dictionary far more attractive to use.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900