Posted by: Sourav | September 22, 2016

Better performance for dictionary in case of lookup compared to list in vb,VB Teacher Sourav,Kolkata 09748184075


in case of lookup in list the lookupup goes through each item to find the search item,however in dictionary the value is found using key,so performance is better in dictionary

 

Module Module1

Sub Main()

Dim country1 As New Country() With { _
.Code = “AUS”, _
.Name = “AUSTRALIA”, _
.Capital = “Canberra” _
}

Dim country2 As New Country() With { _
.Code = “IND”, _
.Name = “INDIA “, _
.Capital = “New Delhi” _
}

Dim country3 As New Country() With { _
.Code = “USA”, _
.Name = “UNITED STATES”, _
.Capital = “Washington D.C.” _
}

Dim country4 As New Country() With { _
.Code = “GBR”, _
.Name = “UNITED KINGDOM”, _
.Capital = “London” _
}

Dim country5 As New Country() With { _
.Code = “CAN”, _
.Name = “CANADA”, _
.Capital = “Ottawa” _
}

Dim listcountries As List(Of Country) = New List(Of Country)
listcountries.Add(country1)
listcountries.Add(country2)
listcountries.Add(country3)
listcountries.Add(country4)
listcountries.Add(country5)
Dim dictionarycountries As Dictionary(Of String, Country) = New Dictionary(Of String, Country)
dictionarycountries.Add(country1.Code, country1)
dictionarycountries.Add(country2.Code, country2)
dictionarycountries.Add(country3.Code, country3)
dictionarycountries.Add(country4.Code, country4)
dictionarycountries.Add(country5.Code, country5)

Dim usrchoice As String = String.Empty
Dim rescountry As Country
‘Do

‘    Console.WriteLine(“Please enter country code”)
‘    Dim strcountrycode As String = Console.ReadLine().ToUpper
‘    Dim rescountry As Country = listcountries.Find(Function(x) x.Code = strcountrycode)
‘    If IsNothing(rescountry) Then
‘        Console.WriteLine(“Country Code not valid”)
‘    Else
‘        Console.WriteLine(“The name of the country is {0} and the name of the capital is {1}”, rescountry.Name, rescountry.Capital)

‘    End If
‘    Do
‘        Console.WriteLine(“Do you want to continue?(yes/no)”)
‘        usrchoice = Console.ReadLine.ToUpper
‘    Loop While usrchoice <> “YES” And usrchoice <> “NO”
‘    ‘If usrchoice = “NO” Then
‘    ‘    Exit Sub
‘    ‘End If
‘Loop While usrchoice = “YES”

Do

Console.WriteLine(“Please enter country code”)
Dim strcountrycode As String = Console.ReadLine().ToUpper
‘If dictionarycountries.ContainsKey(strcountrycode) Then

rescountry = If(dictionarycountries.ContainsKey(strcountrycode), dictionarycountries(strcountrycode), Nothing)

‘Else
‘rescountry = Nothing
‘End If
If IsNothing(rescountry) Then
Console.WriteLine(“Country Code not valid”)
Else
Console.WriteLine(“The name of the country is {0} and the name of the capital is {1}”, rescountry.Name, rescountry.Capital)

End If
Do
Console.WriteLine(“Do you want to continue?(yes/no)”)
usrchoice = Console.ReadLine.ToUpper
Loop While usrchoice <> “YES” And usrchoice <> “NO”
‘If usrchoice = “NO” Then
‘    Exit Sub
‘End If
Loop While usrchoice = “YES”

‘Console.ReadLine()

End Sub

End Module
Public Class Country
Public Property Name() As String
Get
Return m_Name
End Get
Set(ByVal value As String)
m_Name = Value
End Set
End Property
Private m_Name As String
Public Property Code() As String
Get
Return m_Code
End Get
Set(ByVal value As String)
m_Code = Value
End Set
End Property
Private m_Code As String
Public Property Capital() As String
Get
Return m_Capital
End Get
Set(ByVal value As String)
m_Capital = Value
End Set
End Property
Private m_Capital As String
End Class

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: