# F Sharp Programming/Mutable Collections

The .NET BCL comes with its own suite of mutable collections which are found in the System.Collections.Generic namespace. These built-in collections are very similar to their immutable counterparts in F#.

## List<'T> Class

The List<'T> class represents a strongly typed list of objects that can be accessed by index. Conceptually, this makes the List<'T> class similar to arrays. However, unlike arrays, Lists can be resized and don't need to have their size specified on declaration.
.NET lists are created using the new keyword and calling the list's constructor as follows:
It's easy to tell that .NET lists are mutable because their Add methods return unit rather than returning another list.

### Underlying Implementation

Behind the scenes, the List<'T> class is just a fancy wrapper for an array. When a List<'T> is constructed, it creates an 4-element array in memory. Adding the first 4 items is an O(1) operation. However, as soon as the 5th element needs to be added, the list doubles the size of the internal array, which means it has to reallocate new memory and copy elements in the existing list; this is a O(n) operation, where n is the number of items in the list.
The List<'T>.Count property returns the number of items currently held in the collection, the List<'T>.Capacity collection returns the size of the underlying array. This code sample demonstrates how the underlying array is resized:
If you know the maximum size of the list beforehand, it is possible to avoid the performance hit by calling the List<'T>(size : int) constructor instead. The following sample demonstrates how to add 1000 items to a list without resizing the internal array:

## LinkedList<'T> Class

A LinkedList<'T> represented a doubly-linked sequence of nodes which allows efficient O(1) inserts and removal, supports forward and backward traversal, but its implementation prevents efficient random access. Linked lists have a few valuable methods:
Note that these methods return a LinkedListNode<'T>, not a new LinkedList<'T>. Adding nodes actually mutates the LinkedList object:
The Stack<'T> and Queue<'T> classes are special cases of a linked list (they can be thought of as linked lists with restrictions on where you can add and remove items).

### Stack<'T> Class

A Stack<'T> only allows programmers prepend/push and remove/pop items from the front of a list, which makes it a last in, first out (LIFO) data structure. The Stack<'T> class can be thought of as a mutable version of the F# list.
A stack of coins could be represented with this data structure. If we stacked coins one on top another, the first coin in the stack is at the bottom of the stack, and the last coin in the stack appears at the top. We remove coins from top to bottom, so the last coin added to the stack is the first one removed.

### Queue<'T> Class

A Queue<'T> only allows programmers to append/enqueue to the rear of a list and remove/dequeue from the front of a list, which makes it a first in, first out (FIFO) data structure.
A line of people might be represented by a queue: people add themselves to the rear of the line, and are removed from the front. The first person to stand in line is the first person to be served.

## HashSet<'T>, and Dictionary<'TKey, 'TValue> Classes

The HashSet<'T> and Dictionary<'TKey, 'TValue> classes are mutable analogs of the F# set and map data structures and contain many of the same functions.
Using the HashSet<'T>
Using the Dictionary<'TKey, 'TValue>

## Differences Between .NET BCL and F# Collections

The major difference between the collections built into the .NET BCL and their F# analogs is, of course, mutability. The mutable nature of BCL collections dramatically affects their implementation and time-complexity:
| .NET Data Structure | Insert | Remove | Lookup | F# Data Structure | Insert | Remove | Lookup |
| List | O(1) / O(n)* | O(n) | O(1) (by index) / O(n) (linear) | No built-in equivalent | No built-in equivalent | No built-in equivalent | No built-in equivalent |
| LinkedList | O(1) | O(1) | O(n) | No built-in equivalent | No built-in equivalent | No built-in equivalent | No built-in equivalent |
| Stack | O(1) | O(1) | O(n) | List | O(1) | n/a | O(n) |
| Queue | O(1) | O(1) | O(n) | No built-in equivalent | No built-in equivalent | No built-in equivalent | No built-in equivalent |
| HashSet | O(1) / O(n)* | O(1) | O(1) | Set | O(log n) | O(log n) | O(log n) |
| Dictionary | O(1) / O(n)* | O(1) | O(1) | Map | O(log n) | O(log n) | O(log n) |
    * These classes are built on top of internal arrays. They may take a performance hit as the internal arrays are periodically resized when adding items.
    Note: the Big-O notation above refers to the time-complexity of the insert/remove/retrieve operations relative to the number of items in the data structure, not the relative amount of time required to evaluate the operations relative to other data structures. For example, accessing arrays by index vs. accessing dictionaries by key have the same time complexity, O(1), but the operations do not necessarily occur in the same amount of time.