Scroll To Top
Reader Level:

Collections in FSharp

By A K on Oct 19, 2011
Collections allow us to add and remove any number of elements dynamically. Here you will learn about the collection type 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.

System.Collections.Generic //Adding NameSpace

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

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


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
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

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


dictionary<,> in f#

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
hash = new HashSet<string>()

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

elements in hash do
printfn "%s" elements


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

resizearr = new ResizeArray<string>()
//Adding element
//Adding element
//Adding element
//Adding element
//Adding element

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


 resizearray<> in f#