- Back to Home »
- VB.NET »
- VB.NET Collections and Generic Collections - Stack, Queue, List, Dictionary
Posted by :
Sudhir Chekuri
Saturday, 5 October 2013
VB.NET COLLECTIONS
Collections is a type used to store more than one element of different types.
Different Collections are used to store elements and retreive elements in different styles.
VB.NET STACK Collection
Stack is used to store multiple values irrespective of datatypes.
It follows lifo(last in first out).
Push method is used to insert values into stack and pop is used to delete values from stack.
VB.NET stack example program
Module Module1
Sub Main()
'Create object for Stack
Dim sta As New Stack(Of Integer)
'Push()
sta.Push(1)
sta.Push(2)
sta.Push(3)
sta.Push(4)
sta.Push(5)
'Print
For Each i As Integer In sta
Console.WriteLine(i)
Next
'Count
Console.WriteLine("No.of elements in stack " & sta.Count)
'Pop()
Console.WriteLine("Pop element is " & sta.Pop())
'Print
For Each i As Integer In sta
Console.WriteLine(i)
Next
'Peek
Console.WriteLine("Peek element is " & sta.Peek())
'Print
For Each i As Integer In sta
Console.WriteLine(i)
Next
'Count
Console.WriteLine("No.of elements in stack " & sta.Count)
'Clear
sta.Clear()
'Count
Console.WriteLine("No.of elements in stack " & sta.Count)
Console.ReadLine()
End Sub
End Module
Output
5
4
3
2
1
No.of elements in stack 5
Pop element is 5
4
3
2
1 Peek element is 4
4
3
2
1
No.of elements in stack 4
No.of elements in stack 0
Explanation
Push method is used to add elements to Stack.
Pop method is used to remove elements from Stack.
Peek method is used to retreive the topmost element in Stack.
Count method is used to retreive the no. of elements present in a Stack.
VB.NET Queue
Queue is used to store multiple values irrespective of datatypes.
It follows fifo(first in first out).
Enqueue method is used to insert values into queue and Dequeue is used to delete values from queue.
Module Module1
Sub Main()
Dim que As New Queue(Of Integer)
'Enqueue
que.Enqueue(1)
que.Enqueue(2)
que.Enqueue(3)
que.Enqueue(4)
'Print
For Each i As Integer In que
Console.WriteLine(i)
Next
'Count
Console.WriteLine("No.of elements in queue " & que.Count)
'Dequeue
que.Dequeue()
Console.WriteLine("Dequeue element is " & que.Dequeue())
'Print
For Each i As Integer In que
Console.WriteLine(i)
Next
'Count
Console.WriteLine("No.of elements in queue " & que.Count)
'Peek
que.Peek()
Console.WriteLine("Peek element is " & que.Peek())
'Print
For Each i As Integer In que
Console.WriteLine(i)
Next
'Count
Console.WriteLine("No.of elements in queue " & que.Count)
'Clear
que.Clear()
'Count
Console.WriteLine("No.of elements after clearing are " & que.Count)
Console.ReadLine()
End Sub
End Module
Output
1
2
3
4
No.of elements in queue 4
Dequeue element is 2
3
4
No.of elements in queue 2
Peek element is 3
3
4
No.of elements in queue 2
No.of elements after clearing are 0
VB.NET Dictionary
Dictionary is used to store values with keys.
Values can be retreived using keys.
VB.NET Dictionary example program
Module Module1
Sub Main()
Dim dict As New Dictionary(Of Integer, String)
'add()
dict.Add(1, "a")
dict.Add(2, "b")
dict.Add(3, "c")
dict.Add(4, "d")
'Print
For Each j As Integer In dict.Keys
Console.WriteLine(j & dict(j))
Next
'count
Console.WriteLine("No.of items " & dict.Count)
'remove()
dict.Remove(2)
'print
For Each j As Integer In dict.Keys
Console.WriteLine(j & dict(j))
Next
'count
Console.WriteLine("No.of items after removing " & dict.Count)
'clear
dict.Clear()
Console.WriteLine("No.of items " & dict.Count)
Console.ReadLine()
End Sub
End Module
Output
1a
2b
3c
4d
No.of items 4
1a
3c
4d
No.of items after removing 3
No.of items 0
VB.NET List ( Generic Collection )
List is a generic collection used to store multiple elements of specific datatype dynamically using Add method.
VB.NET List example program
Module Module1
Sub Main()
Dim lis As New List(Of String)
'add()
lis.Add("A")
lis.Add("B")
lis.Add("C")
'Print
For Each s As String In lis
Console.WriteLine(s)
Next
'Count
Console.WriteLine("No.of items in list " & lis.Count)
'insert()
lis.Insert(2, "D")
'Console.WriteLine("inserted location is " & lis.Insert())
'print
For Each s As String In lis
Console.WriteLine(s)
Next
'count
Console.WriteLine("No.of items after insertion " & lis.Count)
'remove()
lis.Remove("B")
'print
For Each s As String In lis
Console.WriteLine(s)
Next
'count
Console.WriteLine("No.of items after removing " & lis.Count)
'remove at()
lis.RemoveAt(0)
'print
For Each s As String In lis
Console.WriteLine(s)
Next
'count
Console.WriteLine("No. of items removed " & lis.Count)
'clear
lis.Clear()
'count
Console.WriteLine("No. of items present after clearing " & lis.Count)
Console.ReadLine()
End Sub
End Module
Output
A
B
C
No.of items in list 3
A
B
D
C
No.of items after insertion 4
A
D
C
No.of items after removing 3
D
C
No.of items removed 2
No.of items present after clearing 0.
Collections is a type used to store more than one element of different types.
Different Collections are used to store elements and retreive elements in different styles.
VB.NET STACK Collection
Stack is used to store multiple values irrespective of datatypes.
It follows lifo(last in first out).
Push method is used to insert values into stack and pop is used to delete values from stack.
VB.NET stack example program
Module Module1
Sub Main()
'Create object for Stack
Dim sta As New Stack(Of Integer)
'Push()
sta.Push(1)
sta.Push(2)
sta.Push(3)
sta.Push(4)
sta.Push(5)
For Each i As Integer In sta
Console.WriteLine(i)
Next
'Count
Console.WriteLine("No.of elements in stack " & sta.Count)
'Pop()
Console.WriteLine("Pop element is " & sta.Pop())
For Each i As Integer In sta
Console.WriteLine(i)
Next
'Peek
Console.WriteLine("Peek element is " & sta.Peek())
For Each i As Integer In sta
Console.WriteLine(i)
Next
'Count
Console.WriteLine("No.of elements in stack " & sta.Count)
'Clear
sta.Clear()
'Count
Console.WriteLine("No.of elements in stack " & sta.Count)
Console.ReadLine()
End Sub
End Module
Output
5
4
3
2
1
No.of elements in stack 5
Pop element is 5
4
3
2
1 Peek element is 4
4
3
2
1
No.of elements in stack 4
No.of elements in stack 0
Explanation
Push method is used to add elements to Stack.
Pop method is used to remove elements from Stack.
Peek method is used to retreive the topmost element in Stack.
Count method is used to retreive the no. of elements present in a Stack.
VB.NET Queue
Queue is used to store multiple values irrespective of datatypes.
It follows fifo(first in first out).
Enqueue method is used to insert values into queue and Dequeue is used to delete values from queue.
Module Module1
Sub Main()
Dim que As New Queue(Of Integer)
'Enqueue
que.Enqueue(1)
que.Enqueue(2)
que.Enqueue(3)
que.Enqueue(4)
For Each i As Integer In que
Console.WriteLine(i)
Next
'Count
Console.WriteLine("No.of elements in queue " & que.Count)
'Dequeue
que.Dequeue()
Console.WriteLine("Dequeue element is " & que.Dequeue())
For Each i As Integer In que
Console.WriteLine(i)
Next
'Count
Console.WriteLine("No.of elements in queue " & que.Count)
'Peek
que.Peek()
Console.WriteLine("Peek element is " & que.Peek())
For Each i As Integer In que
Console.WriteLine(i)
Next
'Count
Console.WriteLine("No.of elements in queue " & que.Count)
'Clear
que.Clear()
'Count
Console.WriteLine("No.of elements after clearing are " & que.Count)
Console.ReadLine()
End Sub
End Module
Output
1
2
3
4
No.of elements in queue 4
Dequeue element is 2
3
4
No.of elements in queue 2
Peek element is 3
3
4
No.of elements in queue 2
No.of elements after clearing are 0
VB.NET Dictionary
Dictionary is used to store values with keys.
Values can be retreived using keys.
VB.NET Dictionary example program
Module Module1
Sub Main()
Dim dict As New Dictionary(Of Integer, String)
'add()
dict.Add(1, "a")
dict.Add(2, "b")
dict.Add(3, "c")
dict.Add(4, "d")
For Each j As Integer In dict.Keys
Console.WriteLine(j & dict(j))
Next
'count
Console.WriteLine("No.of items " & dict.Count)
'remove()
dict.Remove(2)
For Each j As Integer In dict.Keys
Console.WriteLine(j & dict(j))
Next
'count
Console.WriteLine("No.of items after removing " & dict.Count)
'clear
dict.Clear()
Console.WriteLine("No.of items " & dict.Count)
Console.ReadLine()
End Sub
End Module
Output
1a
2b
3c
4d
No.of items 4
1a
3c
4d
No.of items after removing 3
No.of items 0
VB.NET List ( Generic Collection )
List is a generic collection used to store multiple elements of specific datatype dynamically using Add method.
VB.NET List example program
Module Module1
Sub Main()
Dim lis As New List(Of String)
'add()
lis.Add("A")
lis.Add("B")
lis.Add("C")
For Each s As String In lis
Console.WriteLine(s)
Next
'Count
Console.WriteLine("No.of items in list " & lis.Count)
'insert()
lis.Insert(2, "D")
'Console.WriteLine("inserted location is " & lis.Insert())
For Each s As String In lis
Console.WriteLine(s)
Next
'count
Console.WriteLine("No.of items after insertion " & lis.Count)
'remove()
lis.Remove("B")
For Each s As String In lis
Console.WriteLine(s)
Next
'count
Console.WriteLine("No.of items after removing " & lis.Count)
'remove at()
lis.RemoveAt(0)
For Each s As String In lis
Console.WriteLine(s)
Next
'count
Console.WriteLine("No. of items removed " & lis.Count)
'clear
lis.Clear()
'count
Console.WriteLine("No. of items present after clearing " & lis.Count)
Console.ReadLine()
End Sub
End Module
Output
A
B
C
No.of items in list 3
A
B
D
C
No.of items after insertion 4
A
D
C
No.of items after removing 3
D
C
No.of items removed 2
No.of items present after clearing 0.