Collections in FSharp

Introduction: Collections allows us to add and remove any number of elements dynamically. Generally we work with an array when we have a fixed number of elements, because an array requires size (number) of elements for allocating memory for them at the time of declaration. If elements are less than the memory size of array then it is a waste of memory. But Collections solve this problem. When we do not know how many elements or items are there, then we use collection types. Collections types are found in System.Collections.Generic namespace. Some of them are given below.

List<'T>: List is similar to array except - there is no need to specify the  size at the time of declaration. Elements or items are added to or retrieved from a List very fast. It dynamically adjusts memory size for elements. Now we write the following code to understand List operations.

open
System.Collections.Generic //Adding NameSpace

let
lst = new List<string>()//Declaring List

lst.Add("A") //Adding element to List
lst.Add("B")//Adding element to List
lst.Add("C")//Adding element to List
lst.Add("D")//Adding element to List
lst.Add("E")//Adding element to List
 

for
elements in lst do//Printing items/elements of List
printfn "%s" elements

Output: 

List<> in f#

Dictionary<'K ,'V>: It contains elements in the form of key - value pairs. A Key is used to retrieved particular value in the List. Its structure is like a hash-table. The following code demonstrates it working.

open System.Collections.Generic //Adding NameSpace
let
dic = new Dictionary<int, string>()//Declaring Dictionary

dic.Add(1,"A") //Adding element to Dictionary
dic.Add(2,"B")//Adding element to Dictionary
dic.Add(3,"C")//Adding element to Dictionary
dic.Add(4,"D")//Adding element to Dictionary
dic.Add(5,"E")//Adding element to Dictionary
 

for
elements in dic do//Printing key/value of List
printfn "Key %d  value %s" elements.Key elements.Value

Output:

dictionary<,> in f#

HashSet<'T>:
It is a collection of different elements. It does not contain the same value twice. So searching for a particular element/item is very easy with a HashSet. We write the following code which is declaring and adding element to HashSet.

open System.Collections.Generic
let
hash = new HashSet<string>()

hash.Add("A") //Adding element
hash.Add("B") //Adding element
hash.Add("C") //Adding element
hash.Add("D") //Adding element

for
elements in hash do
printfn "%s" elements

Output:

HashSet<> in f#

ResizeArray: Resize Array is more efficient than List. It has several important methods like, Add, Count, ConvertAll, Insert, etc which are more efficient. Write the following code.

open System.Collections.Generic //Adding NameSpace
 

let
resizearr = new ResizeArray<string>()
 
resizearr.Add("A")
//Adding element
resizearr.Add("B")
//Adding element
resizearr.Add("C")
//Adding element
resizearr.Add("D")
//Adding element
resizearr.Add("E")
//Adding element

for
elements in resizearr do//Printing elements
printfn " %s" elements

Output:

 resizearray<> in f#

Up Next
    Ebook Download
    View all
    Learn
    View all