Thursday, June 14, 2012

Hashtable Vs Dictionary(of T,T)

I will prefer yo use Dictonary(of T,T)  instead of hastbale because it is type safe and second it will not cast object type  in to appropriate type so performance is better .

Hashtable:

Hashtable is non generic type collection from name space(system.collection)
by default it can contain  any type  means it accept system.object(every type reference or value type derived from system.object class).Hashtable store item in key /value pair. means value are stored in hashcode of key
"key Can't duplicate".

Module Module1

    Sub Main()
        Dim list As New Hashtable
        list.Add(1, "yousuf")
        list.Add(2, "ashar")
        list.Add("third", "captain jack sparrow")
        list.Add(4, "babosa")
        Console.WriteLine("value of key :third")
        Console.WriteLine(list("third"))
        Console.WriteLine("Is Captain Jack Sparrow still in black perl")
        Console.WriteLine(list.ContainsValue("captain jack sparrow"))
        Console.WriteLine("Keys and values In list")
        For Each itm As DictionaryEntry In list
            Console.WriteLine("key : {0} and has value {1}", itm.Key, itm.Value)
        Next
        Console.WriteLine("Keys In list")
        For Each ky As Object In list.Keys
            Console.WriteLine("key : {0} ", ky)
        Next
        Console.WriteLine("values In list")
        For Each vl As Object In list.Values
            Console.WriteLine("value : {0} ", vl)
        Next
        Console.ReadLine()
    End Sub

End Module

Out Put:


Dictonary(of T,T):
Dictonary(of T,T)  is  generic type collection from name space(system.collection.generic)
 it can contain a specific type means that its type safe so that you can't insert an integer and string in same list.
Dim lst As New Dictonary(of T,T)
where "T" can be integer ,string or any type.  if you will assign T as "Object" then it will work same as non generic type.Dictonary(of T,T)  store item in key /value pair. means value are stored in hashcode of key"key Can't duplicate".

Module Module1
    Sub Main()
        Dim list As New Dictionary(Of Integer, String)
        list.Add(1, "yousuf")
        list.Add(2, "ashar")
        list.Add(3, "captain jack sparrow")
        list.Add(4, "babosa")
        Console.WriteLine("value of key :3")
        Console.WriteLine(list(3))
        Console.WriteLine("Is Captain Jack Sparrow still in black perl")
        Console.WriteLine(list.ContainsValue("captain jack sparrow"))
        Console.WriteLine("Keys and values In list")
        For Each itm As KeyValuePair(Of Integer, String) In list
            Console.WriteLine("key : {0} and has value {1}", itm.Key, itm.Value)
        Next
        Console.WriteLine("Keys In list")
        For Each ky As Object In list.Keys
            Console.WriteLine("key : {0} ", ky)
        Next
        Console.WriteLine("values In list")
        For Each vl As Object In list.Values
            Console.WriteLine("value : {0} ", vl)
        Next
        Console.ReadLine()
    End Sub
End Module

Out Put:

No comments:

Post a Comment