Understanding Collections

Why do we need collections
     Usually to store multiple values in a variable we use Array. But arrays have some disadvantage as below

  1. They are fixed in size. It means we cannot increase the size of the array dynamically. 
  2. We cannot insert/delete values to the middle of the array. 

If at all if we want to increase the size of the array then we have to explicitly create new array and then copy all the values from old array to the newly created array. 

There is one more way to increase the size of the array. We can use Array.Resize( )method. This method does same thing which I said in the above point. Only thing is it destroy the old array automatically. 

Hence to overcome all these we should use collections. 

We have two types of collections

  1. Non-Generic collections
  2. Generic Collections
Non generic Collections

Stack
Queues
ArrayList- Mostly commonly used
Hastable
SortedList
LinkedList

Please note : above are classes and they are defined in the namespace called "COLLECTIONS"

Array List - Specialty is that it auto resize the capacity when it's size exceeds when we add values into it. By default, its initial capacity is 4 and at a time it adds 4 room as a memory storage. If this exceeded then it again adds 4 memory storage and so on. 

We can change the default capacity to 10 by passing in its constructor. Then if you exceed 10 items, it will add next 10 memory location and so on. 

The main draw back in array list is - that it is index based access. Why we call it is draw back because for example if I want to store an employee details in an array list and I want to access e-mail Id of an employee then we need to loop entire array list items and then check whether it has email Id. So it is very difficult to access in this kind of context. Hence to overcome this kind of issues we have to go for key/value access based. Ex: Hashtable


IMP NOTE : Even array list are also key/value based access but problem is that here key is index and not string. we cannot change the key here. But In Hashtable we can define our own keys(it can any datatype string, inter, double, float etc. Key is object type and hence we can give any datatype) and store the values

Hash Table - it is also same as array list. It stores as key/value pair. But key here is user defined. The items stored in this collection are not sorted in ascending order. Because for each key is added with hash code which is of type integer. Internally it uses hashing algorithm to store the values


Disadvantage of non generic


They are not type safe. 
Boxing and unboxing is required for each retrieval. 

So to overcome this issue we go for generic

Array - it is type safe but fixed length
Collections - Auto resize but not type safe

We combined both above points and made them as Generic collections

Generic Collections - this is also a collections with type safe and auto resize

List<T> ---->This is replacement of ArrayList

Suppose we want compare any two values which are of any type ( integer, string, double etc). Then if we don’t use generic then for each type we should write separate method.
We can also use user defined types in generics. 

Dictionary<T> -- it is replacement of hashtable 

It is similar way of hashtable we can perform operation. Only thing is hashtable ll not be printed in the order we declared way but dictionary will be in printed in the way we have added order

Icomparable and Icomparer

We can use sort() method on normal data types like integer, float, double and string but if list contains user defined type like student Or customer then compiler it will not sort. 
So in order to sort user defined data types then in the user defined class we need to inherit "Icomparable<user defined class>"


Suppose for example, there is some already defined class developed from some other company. Think like third party class. But they have implemented sorting method on some property ex employeeId but if we want to sort based employeename then we need to use "Icomparer<student>

How sorting works internally
===========================================
Look at two adjacent elements in your array, A and B.
When A <= B: go forward to the next pair.
When A > B: swap A and B, and go back to the previous pair.
When we reach the end, we're done.

1001
1004
1005
1000
1002
A = 1001
B = 1004

Check the conditions
is A <= B ---> yes. So go to next pair
--------------------------------------------

A = 1004
B = 1005

Check the conditions
is A <= B ---> yes. So go to next pair
--------------------------------------------

A = 1005
B = 1000
Check the conditions
is A <= B ---> no. Check next condition

Check the conditions
is A > B ---> yes. Swap the elements and go back to previous pair
-------------------------------------------------------------------------------------
after swaping

1001
1004
1000
1005
1002
A = 1004
B = 1000

Check the conditions
is A <= B ---> no. Check next condition

Check the conditions
is A > B ---> yes. Swap the elements and go back to previous pair
------------------------------------------------------------------------------------
after swaping

1001
1000
1004
1005
1002

A = 1001
B = 1000

Check the conditions
is A <= B ---> no. Check next condition

Check the conditions
is A > B ---> yes. Swap the elements and go back to previous pair
-------------------------------------------------------------------------------------

after swaping

1000
1001
1004
1005
1002

since there is no previous pair we should come from top to bottom

A = 1000
B = 1001

Check the conditions
is A <= B ---> yes. So go to next pair
--------------------------------------------

A = 1001
B = 1004

Check the conditions
is A <= B ---> yes. So go to next pair
--------------------------------------------

A = 1004
B = 1005
Check the conditions
is A <= B ---> yes. So go to next pair
--------------------------------------------

A = 1005
B = 1002

is A <= B ---> no. Check next condition

Check the conditions
is A > B ---> yes. Swap the elements and go back to previous pair
-------------------------------------------------------------------------------------

after swaping

1000
1001
1004
1002
1005

A = 1004
B = 1002

is A <= B ---> no. Check next condition

Check the conditions
is A > B ---> yes. Swap the elements and go back to previous pair
-------------------------------------------------------------------------------------
after swaping

1000
1001
1002
1004
1005

IEnumerable

This is base class for all the collections classes that we have in .NET

IEnumerable
    -ICollections
        -IList------------------->contains all classes which are index based
        -IDictionary------------->contains all classes which are key value pair

if we want to use foreach loop on the class which is not added into the List variable then we must use IEnumerable

Ex: I have an employee class which has list of employees. I can loop this Employee class because, I have added all the employee instance inside the List<> variable. Since List<> variable has inherited IEnumerable interface, we can loop the collections using foreach loop.

Now suppose if a class has not been added into the List<> variable and if you want to loop the class then we need to use IEnumerable interface and then define the GetEnumerator method inside that class.

public IEnumerator GetEnumerator( )
{

       return List<> variable instance.GetEnumerator( )
}

--->Before First [Initially pointer will be here ] 
Emp1
Emp2
Emp3
Emp4
Emp5
Emp6
Afte Last

Comments

Popular posts from this blog

Understanding Collection and Object Initializer

How to execute Javascript in VS code