containers.Map - Object that maps unique keys to values - MATLAB (original) (raw)
Object that maps unique keys to values
Description
A Map
object is a data structure that allows you to retrieve values using a corresponding key. Keys can be real numbers or character vectors. As a result, they provide more flexibility for data access than array indices, which must be positive integers. Values can be scalar or nonscalar arrays.
Creation
Syntax
Description
Note
dictionary is recommended over containers.Map
because it accepts more data types as keys and values and provides better performance. (since R2022b)
M = containers.Map([keySet](#d126e1036130),[valueSet](#d126e1036162))
creates a Map
object that contains keys fromkeySet
, each mapped to a corresponding value fromvalueSet
. The input arguments keySet
and valueSet
must have the same number of elements, withkeySet
having elements that are unique.
M = containers.Map([keySet](#d126e1036130),[valueSet](#d126e1036162),'UniformValues',[isUniform](#d126e1036179))
, where isUniform
is false
, specifies that the values in valueSet
do not need to be uniform. The default value of isUniform
is true
. The values invalueSet
are uniform when they are all scalars that have the same data type, or when they are all character vectors.
M = containers.Map('KeyType',[kType](#d126e1036211),'ValueType',[vType](#d126e1036308))
creates an empty Map
object and specifies the data types of the keys and values you can add to it later. You can switch the order of the'KeyType'
and 'ValueType'
name-value pair arguments, but both name-value pairs are required.
M = containers.Map
creates an emptyMap
object.
Input Arguments
Keys, specified as a numeric array, cell array of character vectors, or string array.
If you specify keys using a string array, then thecontainers.Map
function converts the keys and stores them as character vectors. Because of this conversion, theKeyType
property of the outputMap
object is set to'char'
.
Values, specified as an array.
Indicator of uniform values in valueSet
, specified as true
(1
) orfalse
(0)
.
Data type of the keys to be added to an empty Map
object, specified as a character vector. You can specifykType
as any of the data types in the table, so that keys are either character vectors or numeric scalars.
kType | Data Type and Size of Key |
---|---|
'char' (default) | Character vector |
'double' | Double scalar |
'single' | Single scalar |
'int32' | 32-bit signed integer scalar |
'uint32' | 32-bit unsigned integer scalar |
'int64' | 64-bit signed integer scalar |
'uint64' | 64-bit unsigned integer scalar |
Data type of the values to be added to an empty Map
object, specified as a character vector. You can specifyvType
as any of the data types in the table.
vType | Data Type and Size of Value |
---|---|
'any' (default) | Array that has any data type |
'char' | Character vector |
'logical' | Logical scalar |
'double' | Double scalar |
'single' | Single scalar |
'int8' | 8-bit signed integer scalar |
'uint8' | 8-bit unsigned integer scalar |
'int16' | 16-bit signed integer scalar |
'uint16' | 16-bit unsigned integer scalar |
'int32' | 32-bit signed integer scalar |
'uint32' | 32-bit unsigned integer scalar |
'int64' | 64-bit signed integer scalar |
'uint64' | 64-bit unsigned integer scalar |
Properties
This property is read-only.
Number of key-value pairs in the Map
object, specified as a numeric scalar.
This property is read-only.
Data type of the keys, specified as a character vector.
This property is read-only.
Data type of the values, specified as a character vector.
Object Functions
isKey | Determine if Map object contains key |
---|---|
keys | Return keys of Map object |
length | Number of key-value pairs in Map object |
remove | Delete key-value pairs from Map object |
size | Size of Map object |
values | Return values of Map object |
Examples
Create a Map
object that contains rainfall data for several months. The map contains the four values in valueSet
, and the keys are the four month names in keySet
.
keySet = {'Jan','Feb','Mar','Apr'}; valueSet = [327.2 368.2 197.6 178.4]; M = containers.Map(keySet,valueSet)
M = Map with properties:
Count: 4
KeyType: char
ValueType: double
Display the rainfall for March. You can retrieve the value for March by using 'Mar'
as the key.
Display the number of values in the map. You can access the Count
property using dot notation.
Create a Map
object with identification numbers as keys and employee names as values.
ids = [437 1089 2362]; names = {'Lee, N.','Jones, R.','Sanchez, C.'}; M = containers.Map(ids,names)
M = Map with properties:
Count: 3
KeyType: double
ValueType: char
Retrieve a name using an identification number as a key.
Create a Map
object that contains test results for patients. For any patient, the results might be in either a numeric array or in a file. You can store numeric arrays and file names as values in the same map. To store values that do not have the same data type in the same map, specify 'UniformValues',false
.
keySet = {'Li','Jones','Sanchez'}; testLi = [5.8 7.35]; testJones = [27 3.92 6.4 8.21]; testSanchez = 'C:\Tests\Sanchez.dat';
valueSet = {testLi,testJones,testSanchez}; M = containers.Map(keySet,valueSet,'UniformValues',false)
M = Map with properties:
Count: 3
KeyType: char
ValueType: any
Display the numeric array associated with Li.
Display the file name associated with Sanchez. If the file contains numeric values, you could then call a function to read those values into an array.
ans = 'C:\Tests\Sanchez.dat'
Create an empty Map
object. Specify the data types for key-value pairs added later.
M = containers.Map('KeyType','char','ValueType','double')
M =
Map with properties:
Count: 0
KeyType: char
ValueType: double
Add key-value pairs to the map.
M('Jan') = 327.2; M('Feb') = 368.2; M
M = Map with properties:
Count: 2
KeyType: char
ValueType: double
Display the keys and values that the map now contains.
ans = 1×2 cell {'Feb'} {'Jan'}
ans=1×2 cell array {[368.2000]} {[327.2000]}
Extended Capabilities
Version History
Introduced in R2008b