You are on page 1of 11

hi in this video we are going to cover

collections collection is a very

important topic in any programming as

here we can store the number of elements

all together in a single collection we

have already covered something like that

in arrays as well but as we know and it

provides you a static memory allocation

which may consider the underflow and

overflow by the time you are

implementing so there while using the

collections we can find some classes

which will provide you some specialized

way to store and retrieve the data as

the different classes may have the

different structure data structures to

perform the data storage and whatever

class you will use here will give you

the dynamic memory allocation means the

memory consumption will be on the basis

of the data which you are storing into a

particular collection but any time when

you will store a particular data will be

of object type that means you can assign

any value and you can store the objects

of any type in these collections and to

use the classes of collections you will

have to include the system dot palacios

namespace which will give you some

number of classes like errorless stack q


hash table and sorted list so here in

earliest you will get something like

array and you can anytime remove the

elements from any particular location

along with that you will get stack and Q

which is a very famous data structure

like first in first out and last in

first out we will see all that in the

implementation along with that we have

hash table and sorted list which will

take care of a fair value means having

key and value each value here will be

having a unique key which using which

you can search a particular value and

you can remove the value as well so

let's see quickly how to implement all

these things practically so in

order to implement the collections

in.net framework here first of all I

will have to use system dot collections

namespace which will provide us the

required classes as first of all let's

take an example where I will take the

error list with the name list and here

as soon as I will load this particular

form I will initialize this list and

after that I can add the number of

elements using the add method here as

you can see like it is asking for the


object type of members that means you

can pass any values right here so here I

will pass some strings like as here you

can see I have added five elements one

two three four five inside this list I'm

making sure like I will not pass any

other data type right here in this

particular list but if you want you can

definitely pass as these collections are

not type safe so in order to retrieve

that let's use for each loop or you can

also go for for group in which you can

first of all check the size of the array

by the property called list dot count it

will return you the number of element

and using which you can I trait a for

loop by the time you want in this

program but here I will use this for

each loop since all the elements are of

object type object item in list so right

here I can retrieve each element within

the item now in order to get those

elements in the common controls I will

get a control called list box which I

have just added here alright and what I

can do is list box one dot items dot add

and here I can add the elements which

will come in item okay item in this for

each loop right so let's execute that

and check whether I am getting it or not


so here you can clearly see like all the

five elements has been added in the

listbox after that if you want to remove

the elements from this list at the run

time you can call the remove remove at

and remove range method if you'll say

remove it we'll simply remove a

particular element which you will pass

like if I pass three that particular

element will be removed similarly if you

will call remove add here you will have

to pass the index of the particular

element ofwhich which you want to remove

like here I pass three elements so first

element will be all zero at index then

one two three so this will be removed

and if I want to remove a number of

elements altogether let me comment this

so remove range in the first parameter

you will have to pass the starting index

let's say I pass one as the starting

index and after that I want to remove

three elements so what will happen this

is 0 this is 1 it's starting from this

it will remove three elements that is

two three four will be removed and only

one and five will be there in the list

as you can see here similarly you can

also sort these elements by calling the


sort method right here as you can see

now they are alphabetically sorted in

ascending order if you want to sort them

in the descending order you can just

call the list dot reverse right after

this sort so once it is sorted and you

will reverse it it will be in descending

order now these are some basic

operations if I will discuss a bit about

the memory allocation of this error list

so by default when you take an error

list the capacity is four all right so

let's take an example like I will

comment these lines so as you can see

here like counting capacity are the two

variables which I have taken and Here I

am reading the couple of properties like

count and capacity let me put them in

the message box so as you can see I have

only added two elements here so in count

I will get two and in capacity as I said

by default it is four so I will get that

only so count is two and capacity is

four but if you want you can change the

capacity to any particular number like

here by the time I called the

constructor I passed three so now count

is two and capacity becomes three but is

it is a capacity not the size as soon as

you will exceed the number of elements


from the capacity like here I said three

so as soon as you will add the fourth

element in this list the capacity of

this error list will be double let me

get these messages after the list so as

you can see first of all I have added

these five numbers and let's see what is

the result now so you see count is five

in capacity is six as initially it was

three and as soon as you added the

fourth element it becomes twice that is

three twice is six

but if I will keep it default like four

so the count is still five but capacity

is eight like it's again double of the

four so this is how you can deal with

the memories at runtime as there is

nothing like overloading in case of

these lists but once you are done with

all the operation what you can do is

when you are sure like you are not going

to add any other element or remove any

other element from the list you can

simply trim them using the trim to side

method so again after that I will check

the capacity is equal to list dot

capacity so here you can see before

trimming I am checking the capacity and

even after trimming I am checking the


capacity let's see what the result here

so initially you see the capacity is

eight count is five and after trimming

capacity also becomes five so this is

how you can work with the ArrayList now

here you can see a very simple

implementation for stack as well as here

first of all I have defined the instance

of stack and here right here in the form

load I have pushed some element putting

some elements or adding some elements in

this stack is called the push operation

so that is what I did

now when I will read it using the for

each loop like object as here also all

the elements will be added as the object

so object item in stack and here inside

again I will do this very same thing

list box 1 dot items dot ad so let's see

the output now as for the principle of

stack it is last in first out so I added

it like 1 2 3 4 5 and it is like 5 4 3 2

1 here while working with stack it is

very particular from the for the input

and output so the sorting operation will

not be possible here even though if you

want to remove the elements you will not

get so many options as you got in the

error list here as we know pop is

something which helps us to remove the


element from the stack and the topmost

elements means the last elements will

get popped so if I will call a stack dot

pop so 5 will be the topmost element

from the stack and it will get removed

as here you can see 4 3 2 1 are the

remaining elements you can use the peek

method as it will return an object I

store that in a variable called talk and

if you want you can read that like top

dot to string so when I will execute

that you see 4 is the topmost element as

I popped out the 5 so 4 is the top and

then again as you can see it is not

removed and it is still in this list so

it's all about your stack and a very

similar implementation for the queue

itself like here Q is a new element

which are initialized right here in the

form load when you add some element

inside a queue it is called NQ and when

you delete some element it is DQ so it

is first in first out so I added it like

1 2 3 4 5 and when I will retrieve this

you will see it is in the very same

sequence as when you will DQ the first

element in the queue will be removed and

here the first element is this same

which first you entered so when I will


say Q dot DQ it

remove the first element as you can see

there is no overloading so by default

the first element which is one in this

particular data structure is there so

when I will execute it one is DQ'd and

now the remaining ones are here now here

the next is hash-table

as we have discussed in the description

earlier that hash table will carry a key

value pair so whenever I will start

adding any element right here it is

asking for a couple of values like

object key object value so whenever you

will enter the key the key must be

unique

duplicate key will not be allowed as all

the searching here will be done on the

basis of the key itself so here let's

say I'll write key one and then its

value and similarly I will add some more

elements so I have added five elements

here with the different keys and values

as you see values are duplicated here

but it will not be an issue but you see

keys are unique every time I added a new

record now to retrieve this basically

whenever I will check the structure of

this single value this is something what

we call a key value pair means this is


not a single value they are the couple

of values and here in this particular

case we will call it dictionary entry so

when I will use a for each loop here I

will call it a dictionary entry I term

in HT that is the hash table and now

inside this you see if I will say item

dot so you will get key and value both

separately all right because item is of

type dictionary entry which will take

care of all this

paired values so firstly I will say key

then a separator colon and then again

the value so when I will execute this

you see all the values have been printed

right here in order to remove a

particular value you can pass that

particular key like here if I will say

Kevon all right in the very see

they wear the same case I'll have to

write so if I will execute it again you

will not find key one again in this list

similarly if you want to search the

element from this hash table you can

call this contains method which will

return a boolean and it will ask for a

particular key whether that particular

key is there or not as I said this key

is going to help us whenever I want to


search a particular element so using

these methods you can simply start

working with these hash table also and

all these same features and

functionalities you will get in sorted

list as well as that of the hash table

because it also gives you the key value

pair the only difference is here all the

data will be sorted on the basis of

these key so when I will execute that

you see all the data is in this sorted

order right here this is how you can

start working with these collections

You might also like