Object that maps unique keys to values (2024)

Object that maps unique keys to values

expand all in page

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

M = containers.Map(keySet,valueSet)

M = containers.Map(keySet,valueSet,'UniformValues',isUniform)

M = containers.Map('KeyType',kType,'ValueType',vType)

M = containers.Map

Description

Note

dictionary is recommended over containers.Map because it accepts more data types as keys and values and provides better performance. (since R2022b)

example

M = containers.Map(keySet,valueSet) creates a Map object that contains keys from keySet, each mapped to a corresponding value from valueSet. The input arguments keySet and valueSet must have the same number of elements, with keySet having elements that are unique.

example

M = containers.Map(keySet,valueSet,'UniformValues',isUniform), 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 in valueSet are uniform when they are all scalars that have the same data type, or when they are all character vectors.

example

M = containers.Map('KeyType',kType,'ValueType',vType) 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 empty Map object.

Input Arguments

expand all

Keys, specified as a numeric array, cell array of character vectors, or string array.

If you specify keys using a string array, then the containers.Map function converts the keys and stores them as character vectors. Because of this conversion, the KeyType property of the output Map object is set to 'char'.

Values, specified as an array.

Indicator of uniform values in valueSet, specified as true (1) or false (0).

Data type of the keys to be added to an empty Map object, specified as a character vector. You can specify kType 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 specify vType 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

expand all

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

isKeyDetermine if Map object contains key
keysReturn keys of Map object
lengthNumber of key-value pairs in Map object
removeDelete key-value pairs from Map object
sizeSize of Map object
valuesReturn values of Map object

Examples

collapse all

Create Map

Open Live Script

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.

M('Mar')

Display the number of values in the map. You can access the Count property using dot notation.

M.Count
ans = uint64 4

Specify Numbers as Keys

Open Live Script

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.

M(437)
ans = 'Lee, N.'

Store Values That Are Not Uniform

Open Live Script

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.

M('Li')
ans = 1×2 5.8000 7.3500

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.

M('Sanchez')
ans = 'C:\Tests\Sanchez.dat'

Specify Types for Empty Map

Open Live Script

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.

keys(M)
ans = 1x2 cell {'Feb'} {'Jan'}
values(M)
ans=1×2 cell array {[368.2000]} {[327.2000]}

Extended Capabilities

Version History

Introduced in R2008b

See Also

dictionary | keys | isKey | values | cell | struct | table

Topics

  • Map Data with Dictionaries

Comando de MATLAB

Ha hecho clic en un enlace que corresponde a este comando de MATLAB:

 

Ejecute el comando introduciéndolo en la ventana de comandos de MATLAB. Los navegadores web no admiten comandos de MATLAB.

Object that maps unique keys to values (1)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

Contact your local office

Object that maps unique keys to values (2024)

FAQs

Object that maps unique keys to values? ›

A dictionary is a map that stores data as values, which can be accessed using corresponding unique keys. Each pair of keys and values is an entry.

How do you create a key value pair map? ›

How to Add a key/value Pair to Map in JavaScript ?
  1. Map constructor.
  2. Using map set() Method.
  3. Using Map object and spread operator.
  4. Using Array map() Method.
  5. Using Object.entries() and Spread Operator.
  6. Using Array.prototype.forEach()
Jun 10, 2024

Is there a HashMap in Matlab? ›

MATLAB HashMap Implementation

Some data types are not supported at all (structs, function handles, etc). This implementation gives you more of a modern implementation of hash maps, allowing mixing of data types, functions/structs/classes as map keys, and concise language to improve code readability.

How do you check if a key is in a map in Matlab? ›

TF = isKey( M , keySet ) returns 1 ( true ) if M contains the specified key, and returns 0 ( false ) otherwise. If keySet is an array that specifies multiple keys, then TF is a logical array of the same size.

Is key matlab? ›

tf = isKey( d , key ) returns a logical 1 ( true ) if the configured dictionary contains the specified key, and it returns a logical 0 ( false ) otherwise. If d is an unconfigured dictionary, isKey throws an error. If key is an array that specifies multiple keys, then tf is a logical array of the same size.

How do I map a single key to multiple values? ›

When we encounter a situation where we need to store multiple values under the same key, we can consider various approaches to address this challenge. One common solution is to use a data structure like an ArrayList, LinkedList, or HashSet to store the multiple values for each key.

How do I get values from a map based on a key? ›

HashMap get() Method in Java

get() method of HashMap class is used to retrieve or fetch the value mapped by a particular key mentioned in the parameter. It returns NULL when the map contains no such mapping for the key.

How do I check if a map contains a value for a key? ›

The containsKey() method is part of the Java Map interface. It is used to check if a map contains a mapping for a specified key.

What does the key map show? ›

Maps use a key, or legend, to explain the meaning of each of the symbols used in the map. These keys usually show a small picture of each of the symbols used on the map, along with a written description of the meaning of each of these symbols.

How do you check whether an object has a key? ›

“The best way to check the presence of a key in an object is by using the in operator or the hasOwnProperty() method.” The in the operator looks for a key within an object or its prototypes. It gives back true if the key is present and false if it isn't. You use it like this: 'key' in object .

Is MATLAB still a thing? ›

Millions of engineers and scientists worldwide use MATLAB for a range of applications, in industry and academia, including deep learning and machine learning, signal processing and communications, image and video processing, control systems, test and measurement, computational finance, and computational biology.

Is MATLAB real coding? ›

MATLAB is a high-level programming language designed for engineers and scientists that expresses matrix and array mathematics directly.

How do I get all the keys in MATLAB? ›

k = keys( c ) returns a list of all the keys in a data cache as a cell array.

How do you write a key-value pair? ›

A key-value pair consists of two related data elements: A key, which is a constant that defines the data set (e.g., gender, color, price), and a value, which is a variable that belongs to the set (e.g., male/female, green, 100). Fully formed, a key-value pair could look like these: gender = male. color = green.

How can a key-value pair is formed? ›

A key-value pair is a data type that includes two pieces of data that have a set of associated values and a group of key identifiers. Within a key-value pair, there are two related data elements. The first is a constant used to define the data set. The other is a value, which is a variable belonging to the data set.

How do you set a value for a key in a map? ›

You can add values to a map with the set() method. The first argument will be the key, and the second argument will be the value. This example looks similar to a regular object with string-based keys, but we can use any data type as a key with Map.

How to add a key-value pair to a map in Java? ›

Java HashMap put() is a method that is used to add key/value pairs inside a hashmap. Java HashMap put() method returns the previously stored value for the key if it was already present in the hashmap. Otherwise, it adds the key/value and returns null.

Top Articles
Latest Posts
Article information

Author: Greg O'Connell

Last Updated:

Views: 6614

Rating: 4.1 / 5 (62 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Greg O'Connell

Birthday: 1992-01-10

Address: Suite 517 2436 Jefferey Pass, Shanitaside, UT 27519

Phone: +2614651609714

Job: Education Developer

Hobby: Cooking, Gambling, Pottery, Shooting, Baseball, Singing, Snowboarding

Introduction: My name is Greg O'Connell, I am a delightful, colorful, talented, kind, lively, modern, tender person who loves writing and wants to share my knowledge and understanding with you.