COLLECTION
Collection is a specialized array which is meant to help the developers by reducing their time of writing the array code. I have explained six classes of Collection below.
- ArrayList
- Queue
- Stack
- Hashtable
- Sorted List
- BitArray
ArrayList
ArrayList is the most basic, simple, and un-ordered collection. ArrayList is used to add and remove items in a very simple and straight forward way. The following points will make this topic easier to understand.
- ArrayList is the most basic class of all classes.
- You can also use indexer to set a specific object in a Collection.
- ArrayLists are dynamic collections. They support “Add Method”, “AddRange Method”. Also, the ArrayList also supports inserting an object in a specific location; “Insert method” is used to accomplish this task.
- Add method allows you to add a single item of any type in a collection.
- AddRange method is used to add a range of an item, usually added from another collection or an array.
- Insert method is used to add items on specific location.
- Remove method is used to remove a single item from the collection.
- ArrayList supports three methods for removing the items, “Remove method”, “RemoveAt method” and “RemoveRange method”.
- RemoveAt method allows you to remove an item in a particular index. Whereas RemoveRange method is used to remove a range of the indexes, at once, present in collection.
The following pictorial view will help you understand the ArrayList in an easier way.
- Queue treats objects like bank treats its customers in line.
- Queue stores the data on sequential basis. That’s why it is called sequential collection also.
- Queue allows duplicate items.
- Queue allows null values.
- Queue’s most famous three methods are “peek”, “dequeue”, and “enqueue”.
- Enqueue method is used to add item in collection.
- Dequeue method retrieves an item from the front of the queue and removes it at the same time.
- Peek method is used to retrieve data from the list.
This diagram will help you understand the queue class in an easier way.
Examples
Stack
Stack is a "Last-In, First-Out" collection. Its best examples include guns, plats, and playing cards etc. After making instance, you can use the methods of stack. Following are the behaviors of stack which help you understand the stack.
- Stack allows duplicate values.
- Stack allows null values.
- Stack is also a sequential collection, just like queue.
- Stack supports three famous method which are known as POP, PUSH and PEEK.
- POP is used to remove the item from the stack after retrieving it from the top of the stack.
- PUSH is used to add items in the stack.
- PEEK is used to retrieve items from the stack.
The following image will help you understand the stack class in an easier way.
Examples
Hash table
Hashtables are the combination of rows and columns, with some keys and values. The following points will help you understand the hashtable more easily.
- It uses integer value which is called hash.
- Hash is used to speed up the searching for the specific key in collection.
- Keys are case sensitive and cannot be null in hash table.
- If you try to store the same key twice, then it will replace the first call by second call.
- Values can be duplicated in hash tables.
- It has no fixed data types.
- It uses namespaces like system.Collection.
The following diagrammatic view will help you understand the hashtable in an easier way.
Examples
Sorted List
It is clear from its name that this class of collection is used for sorted data. It slightly works like hash table. Some of its properties are given below.
- It always gives sorted result.
- It uses key and value pair like hashtable.
- You can also add values directly without assigning key in sorted list.
- It uses RemoveAt method to delete specific value.
It uses Contain Keys to determine whether the collection contains the specific key or not.
Examples
Bit Array
Bit array is a specialized array meant to deal with Boolean expressions. Bit array is a class level thing which needs instance. Following points will help you understand the bit array easily.
- Bit array deals with the Boolean expression.
- By default, the value of Boolean expression in bit array is false.
- You can set the value of Boolean expression by using indexer.
- Bit array also support common operation such as AND, NOT, OR & Xor operation.
- Bit array class is a resizable class that can store Boolean expression.
- We define the size of bit array at the same time when we create its instance.
- Bit array does not support ADD & REMOVE methods just because each value in bit array can only be true or false.
Here is the diagrammatic view of bit array which will help you understand the bit array class more easily.