Header menu logo FSharp.Core

Map<'Key, 'Value> Type

Immutable maps based on binary trees, where keys are ordered by F# generic comparison. By default comparison is the F# structural comparison function or uses implementations of the IComparable interface on key values.

See the Map module for further operations on maps. All members of this class are thread-safe and may be used concurrently from multiple threads.

Constructors

Constructor Description

Map(elements)

Full Usage: Map(elements)

Parameters:
    elements : ('Key * 'Value) seq - The input sequence of key/value pairs.

Returns: Map<'Key, 'Value> The resulting map.

Builds a map that contains the bindings of the given IEnumerable.

elements : ('Key * 'Value) seq

The input sequence of key/value pairs.

Returns: Map<'Key, 'Value>

The resulting map.

Example

 Map [ (1, "a"); (2, "b") ] // evaluates to map [(1, "a"); (2, "b")]
Multiple items
module Map from Microsoft.FSharp.Collections

--------------------
type Map<'Key,'Value (requires comparison)> = interface IReadOnlyDictionary<'Key,'Value> interface IReadOnlyCollection<KeyValuePair<'Key,'Value>> interface IEnumerable interface IStructuralEquatable interface IComparable interface IEnumerable<KeyValuePair<'Key,'Value>> interface ICollection<KeyValuePair<'Key,'Value>> interface IDictionary<'Key,'Value> new: elements: ('Key * 'Value) seq -> Map<'Key,'Value> member Add: key: 'Key * value: 'Value -> Map<'Key,'Value> ...

--------------------
new: elements: ('Key * 'Value) seq -> Map<'Key,'Value>

Instance members

Instance member Description

this.Add

Full Usage: this.Add

Parameters:
    key : 'Key - The key to add.
    value : 'Value - The value to add.

Returns: Map<'Key, 'Value> The resulting map.

Returns a new map with the binding added to the given map. If a binding with the given key already exists in the input map, the existing binding is replaced by the new binding in the result map.

key : 'Key

The key to add.

value : 'Value

The value to add.

Returns: Map<'Key, 'Value>

The resulting map.

Example

 let sample = Map [ (1, "a"); (2, "b") ]

 sample.Add (3, "c") // evaluates to map [(1, "a"); (2, "b"); (3, "c")]
 sample.Add (2, "aa") // evaluates to map [(1, "a"); (2, "aa")]
val sample: Map<int,string>
Multiple items
module Map from Microsoft.FSharp.Collections

--------------------
type Map<'Key,'Value (requires comparison)> = interface IReadOnlyDictionary<'Key,'Value> interface IReadOnlyCollection<KeyValuePair<'Key,'Value>> interface IEnumerable interface IStructuralEquatable interface IComparable interface IEnumerable<KeyValuePair<'Key,'Value>> interface ICollection<KeyValuePair<'Key,'Value>> interface IDictionary<'Key,'Value> new: elements: ('Key * 'Value) seq -> Map<'Key,'Value> member Add: key: 'Key * value: 'Value -> Map<'Key,'Value> ...

--------------------
new: elements: ('Key * 'Value) seq -> Map<'Key,'Value>
member Map.Add: key: 'Key * value: 'Value -> Map<'Key,'Value>

this.Change

Full Usage: this.Change

Parameters:
    key : 'Key - The input key.
    f : 'Value option -> 'Value option - The change function.

Returns: Map<'Key, 'Value> The resulting map.

Returns a new map with the value stored under key changed according to f.

key : 'Key

The input key.

f : 'Value option -> 'Value option

The change function.

Returns: Map<'Key, 'Value>

The resulting map.

Example

 let sample = Map [ (1, "a"); (2, "b") ]

 let f x =
     match x with
     | Some s -> Some (s + "z")
     | None -> None

 sample.Change (1, f) // evaluates to map [(1, "az"); (2, "b")]
val sample: Map<int,string>
Multiple items
module Map from Microsoft.FSharp.Collections

--------------------
type Map<'Key,'Value (requires comparison)> = interface IReadOnlyDictionary<'Key,'Value> interface IReadOnlyCollection<KeyValuePair<'Key,'Value>> interface IEnumerable interface IStructuralEquatable interface IComparable interface IEnumerable<KeyValuePair<'Key,'Value>> interface ICollection<KeyValuePair<'Key,'Value>> interface IDictionary<'Key,'Value> new: elements: ('Key * 'Value) seq -> Map<'Key,'Value> member Add: key: 'Key * value: 'Value -> Map<'Key,'Value> ...

--------------------
new: elements: ('Key * 'Value) seq -> Map<'Key,'Value>
val f: x: string option -> string option
val x: string option
union case Option.Some: Value: 'T -> Option<'T>
val s: string
union case Option.None: Option<'T>
member Map.Change: key: 'Key * f: ('Value option -> 'Value option) -> Map<'Key,'Value>

this.ContainsKey

Full Usage: this.ContainsKey

Parameters:
    key : 'Key - The input key.

Returns: bool True if the map contains the given key.

Tests if an element is in the domain of the map.

key : 'Key

The input key.

Returns: bool

True if the map contains the given key.

Example

 let sample = Map [ (1, "a"); (2, "b") ]

 sample.ContainsKey 1 // evaluates to true
 sample.ContainsKey 3 // evaluates to false
val sample: Map<int,string>
Multiple items
module Map from Microsoft.FSharp.Collections

--------------------
type Map<'Key,'Value (requires comparison)> = interface IReadOnlyDictionary<'Key,'Value> interface IReadOnlyCollection<KeyValuePair<'Key,'Value>> interface IEnumerable interface IStructuralEquatable interface IComparable interface IEnumerable<KeyValuePair<'Key,'Value>> interface ICollection<KeyValuePair<'Key,'Value>> interface IDictionary<'Key,'Value> new: elements: ('Key * 'Value) seq -> Map<'Key,'Value> member Add: key: 'Key * value: 'Value -> Map<'Key,'Value> ...

--------------------
new: elements: ('Key * 'Value) seq -> Map<'Key,'Value>
member Map.ContainsKey: key: 'Key -> bool

this.Count

Full Usage: this.Count

Returns: int

The number of bindings in the map.

Returns: int
Example

 let sample = Map [ (1, "a"); (2, "b") ]

 sample.Count // evaluates to 2
val sample: Map<int,string>
Multiple items
module Map from Microsoft.FSharp.Collections

--------------------
type Map<'Key,'Value (requires comparison)> = interface IReadOnlyDictionary<'Key,'Value> interface IReadOnlyCollection<KeyValuePair<'Key,'Value>> interface IEnumerable interface IStructuralEquatable interface IComparable interface IEnumerable<KeyValuePair<'Key,'Value>> interface ICollection<KeyValuePair<'Key,'Value>> interface IDictionary<'Key,'Value> new: elements: ('Key * 'Value) seq -> Map<'Key,'Value> member Add: key: 'Key * value: 'Value -> Map<'Key,'Value> ...

--------------------
new: elements: ('Key * 'Value) seq -> Map<'Key,'Value>
property Map.Count: int with get

this.IsEmpty

Full Usage: this.IsEmpty

Returns: bool

Returns true if there are no bindings in the map.

Returns: bool
Example

 let emptyMap: Map<int, string> = Map.empty
 emptyMap.IsEmpty // evaluates to true

 let notEmptyMap = Map [ (1, "a"); (2, "b") ]
 notEmptyMap.IsEmpty // evaluates to false
val emptyMap: Map<int,string>
Multiple items
module Map from Microsoft.FSharp.Collections

--------------------
type Map<'Key,'Value (requires comparison)> = interface IReadOnlyDictionary<'Key,'Value> interface IReadOnlyCollection<KeyValuePair<'Key,'Value>> interface IEnumerable interface IStructuralEquatable interface IComparable interface IEnumerable<KeyValuePair<'Key,'Value>> interface ICollection<KeyValuePair<'Key,'Value>> interface IDictionary<'Key,'Value> new: elements: ('Key * 'Value) seq -> Map<'Key,'Value> member Add: key: 'Key * value: 'Value -> Map<'Key,'Value> ...

--------------------
new: elements: ('Key * 'Value) seq -> Map<'Key,'Value>
Multiple items
val int: value: 'T -> int (requires member op_Explicit)

--------------------
type int = int32

--------------------
type int<'Measure> = int
Multiple items
val string: value: 'T -> string

--------------------
type string = System.String
val empty<'Key,'T (requires comparison)> : Map<'Key,'T> (requires comparison)
property Map.IsEmpty: bool with get
val notEmptyMap: Map<int,string>

this[key]

Full Usage: this[key]

Parameters:
    key : 'Key - The input key.

Returns: 'Value The value mapped to the key.

Lookup an element in the map. Raise KeyNotFoundException if no binding exists in the map.

key : 'Key

The input key.

Returns: 'Value

The value mapped to the key.

KeyNotFoundException Thrown when the key is not found.
Example

 let sample = Map [ (1, "a"); (2, "b") ]

 sample.[1] // evaluates to "a"
 sample.[3] // throws KeyNotFoundException
val sample: Map<int,string>
Multiple items
module Map from Microsoft.FSharp.Collections

--------------------
type Map<'Key,'Value (requires comparison)> = interface IReadOnlyDictionary<'Key,'Value> interface IReadOnlyCollection<KeyValuePair<'Key,'Value>> interface IEnumerable interface IStructuralEquatable interface IComparable interface IEnumerable<KeyValuePair<'Key,'Value>> interface ICollection<KeyValuePair<'Key,'Value>> interface IDictionary<'Key,'Value> new: elements: ('Key * 'Value) seq -> Map<'Key,'Value> member Add: key: 'Key * value: 'Value -> Map<'Key,'Value> ...

--------------------
new: elements: ('Key * 'Value) seq -> Map<'Key,'Value>

this.Keys

Full Usage: this.Keys

Returns: ICollection<'Key>

The keys in the map. The sequence will be ordered by the keys of the map.

Returns: ICollection<'Key>
Example

 let sample = Map [ (1, "a"); (2, "b") ]

 sample.Keys // evaluates to seq [1; 2]
val sample: Map<int,string>
Multiple items
module Map from Microsoft.FSharp.Collections

--------------------
type Map<'Key,'Value (requires comparison)> = interface IReadOnlyDictionary<'Key,'Value> interface IReadOnlyCollection<KeyValuePair<'Key,'Value>> interface IEnumerable interface IStructuralEquatable interface IComparable interface IEnumerable<KeyValuePair<'Key,'Value>> interface ICollection<KeyValuePair<'Key,'Value>> interface IDictionary<'Key,'Value> new: elements: ('Key * 'Value) seq -> Map<'Key,'Value> member Add: key: 'Key * value: 'Value -> Map<'Key,'Value> ...

--------------------
new: elements: ('Key * 'Value) seq -> Map<'Key,'Value>
property Map.Keys: System.Collections.Generic.ICollection<int> with get

this.Remove

Full Usage: this.Remove

Parameters:
    key : 'Key - The input key.

Returns: Map<'Key, 'Value> The resulting map.

Removes an element from the domain of the map. No exception is raised if the element is not present.

key : 'Key

The input key.

Returns: Map<'Key, 'Value>

The resulting map.

Example

 let sample = Map [ (1, "a"); (2, "b") ]

 sample.Remove 1 // evaluates to map [(2, "b")]
 sample.Remove 3 // equal to sample
val sample: Map<int,string>
Multiple items
module Map from Microsoft.FSharp.Collections

--------------------
type Map<'Key,'Value (requires comparison)> = interface IReadOnlyDictionary<'Key,'Value> interface IReadOnlyCollection<KeyValuePair<'Key,'Value>> interface IEnumerable interface IStructuralEquatable interface IComparable interface IEnumerable<KeyValuePair<'Key,'Value>> interface ICollection<KeyValuePair<'Key,'Value>> interface IDictionary<'Key,'Value> new: elements: ('Key * 'Value) seq -> Map<'Key,'Value> member Add: key: 'Key * value: 'Value -> Map<'Key,'Value> ...

--------------------
new: elements: ('Key * 'Value) seq -> Map<'Key,'Value>
member Map.Remove: key: 'Key -> Map<'Key,'Value>

this.TryFind

Full Usage: this.TryFind

Parameters:
    key : 'Key - The input key.

Returns: 'Value option The mapped value, or None if the key is not in the map.

Lookup an element in the map, returning a Some value if the element is in the domain of the map and None if not.

key : 'Key

The input key.

Returns: 'Value option

The mapped value, or None if the key is not in the map.

Example

 let sample = Map [ (1, "a"); (2, "b") ]

 sample.TryFind 1 // evaluates to Some "a"
 sample.TryFind 3 // evaluates to None
val sample: Map<int,string>
Multiple items
module Map from Microsoft.FSharp.Collections

--------------------
type Map<'Key,'Value (requires comparison)> = interface IReadOnlyDictionary<'Key,'Value> interface IReadOnlyCollection<KeyValuePair<'Key,'Value>> interface IEnumerable interface IStructuralEquatable interface IComparable interface IEnumerable<KeyValuePair<'Key,'Value>> interface ICollection<KeyValuePair<'Key,'Value>> interface IDictionary<'Key,'Value> new: elements: ('Key * 'Value) seq -> Map<'Key,'Value> member Add: key: 'Key * value: 'Value -> Map<'Key,'Value> ...

--------------------
new: elements: ('Key * 'Value) seq -> Map<'Key,'Value>
member Map.TryFind: key: 'Key -> 'Value option

this.TryGetValue

Full Usage: this.TryGetValue

Parameters:
    key : 'Key - The input key.
    value : byref<'Value> - A reference to the output value.

Returns: bool true if the value is present, false if not.

Lookup an element in the map, assigning to value if the element is in the domain of the map and returning false if not.

key : 'Key

The input key.

value : byref<'Value>

A reference to the output value.

Returns: bool

true if the value is present, false if not.

Example

 let sample = Map [ (1, "a"); (2, "b") ]

 sample.TryGetValue 1 // evaluates to (true, "a")
 sample.TryGetValue 3 // evaluates to (false, null)

 let mutable x = ""
 sample.TryGetValue (1, &x) // evaluates to true, x set to "a"

 let mutable y = ""
 sample.TryGetValue (3, &y) // evaluates to false, y unchanged
val sample: Map<int,string>
Multiple items
module Map from Microsoft.FSharp.Collections

--------------------
type Map<'Key,'Value (requires comparison)> = interface IReadOnlyDictionary<'Key,'Value> interface IReadOnlyCollection<KeyValuePair<'Key,'Value>> interface IEnumerable interface IStructuralEquatable interface IComparable interface IEnumerable<KeyValuePair<'Key,'Value>> interface ICollection<KeyValuePair<'Key,'Value>> interface IDictionary<'Key,'Value> new: elements: ('Key * 'Value) seq -> Map<'Key,'Value> member Add: key: 'Key * value: 'Value -> Map<'Key,'Value> ...

--------------------
new: elements: ('Key * 'Value) seq -> Map<'Key,'Value>
member Map.TryGetValue: key: 'Key * [<System.Runtime.InteropServices.Out>] value: byref<'Value> -> bool
val mutable x: string
val mutable y: string

this.Values

Full Usage: this.Values

Returns: ICollection<'Value>

All the values in the map, including the duplicates. The sequence will be ordered by the keys of the map.

Returns: ICollection<'Value>
Example

 let sample = Map [ (1, "a"); (2, "b") ]

 sample.Values // evaluates to seq ["a"; "b"]
val sample: Map<int,string>
Multiple items
module Map from Microsoft.FSharp.Collections

--------------------
type Map<'Key,'Value (requires comparison)> = interface IReadOnlyDictionary<'Key,'Value> interface IReadOnlyCollection<KeyValuePair<'Key,'Value>> interface IEnumerable interface IStructuralEquatable interface IComparable interface IEnumerable<KeyValuePair<'Key,'Value>> interface ICollection<KeyValuePair<'Key,'Value>> interface IDictionary<'Key,'Value> new: elements: ('Key * 'Value) seq -> Map<'Key,'Value> member Add: key: 'Key * value: 'Value -> Map<'Key,'Value> ...

--------------------
new: elements: ('Key * 'Value) seq -> Map<'Key,'Value>
property Map.Values: System.Collections.Generic.ICollection<string> with get

Type something to start searching.