Friday, November 6, 2015

Collection - Still working on... ! :)

Collections

Dynamic Array

Fast lookup

Required Cast

Good Choice for Booleans

Thread safety

Stores Any type object

Linear Array

Loop

Comments

List

Yes

Not good

No

No

no

No(Except List<object> )

Yes

It is fast because it is Linear

 

Dictionary

Yes

Yes

No

No

No

 

 

 

 

ArrayList

yes

No

Yes

No

No

Yes

Yes

 

 

Hashtable

 

Yes

 

 

 

 

 

 

It is an older .NET Framework type.

ConcurrentDictionary 

Yes

Yes

No

No

Yes

No

 

 

 

ConcurrentBag

 

 

 

 

 

 

 

 

Sometimes lookup functionality is not required. ConcurrentBag allows us to safely Add and Take results

BitArray

 

 

 

yes

 

 

 

 

The program creates a bool array with true and false values, and then the BitArray constructor converts those into one bit each. This means that instead of one byte for a bool, the values are stored as one bit, in one-eighth the space.

Tuple

 

 

 

 

 

 

 

 

A Tuple has many items. Each item can have any type. The Tuple class provides a unified syntax for creating objects with typed fields.

KeyValuePair

 

 

 

 

 

 

 

 

Tuple was faster in every test than KeyValuePair except in allocation performance.

Stack

 

 

 

 

 

 

 

 

 

Queue

 

 

 

 

 

 

 

 

 

HashSet

 

 

 

 

 

 

 

 

HashSet is an optimized set collection. It helps eliminates duplicate strings or elements in an array. It provides a simple syntax for taking the union of elements in a set. This is performed in its constructor.

Constructor

 

Example. This program contains a source array that contains several duplicated strings. It eliminates duplicate strings in the array. The program calls the HashSet constructor to transform the array elements into a set data structure.

 

When the source input becomes large with thousands of elements, hashed collections are faster.

 

SortedSet

 

 

 

 

 

 

 

 

SortedSet is an ordered set collection. We have many elements and want to store them in a sorted order and also eliminate all duplicates from the data structure. The SortedSet is part of the System.Collections.Generic namespace.

 

 

 

 

 

 

 

 

 

Lazy

 

 

 

 

 

 

 

 

This type implements the lazy instantiation pattern. We look into the concept of thunks, as described in a programming textbook. The Lazy class has no relation to the Sleep method.

HybridDictionary

 

 

 

 

 

 

 

 

HybridDictionary attempts to optimize Hashtable. It implements a linked list and hash table data structure, switching over to the second from the first when the number of elements increases past a certain threshold.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

                                                                                                                                                                                                                                                        

No comments:

Post a Comment