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

MATLAB Command

You clicked a link that corresponds to this MATLAB command:

 

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

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

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本 (日本語)
  • 한국 (한국어)

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.

What data structure is used to store key value pairs in Java? ›

A hashmap is a structure able to store key-value pairs. The value is any object your application needs to handle, and a key is something that can represent this object. Suppose you need to create an application that has to handle invoices, represented by instances of an Invoice class.

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.

How do you assign a key-value pair? ›

To add a key-value pair to a dictionary in Python, you assign a value to a new or existing key using the assignment operator = .

Does Map allow duplicate keys and values? ›

An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value. This interface takes the place of the Dictionary class, which was a totally abstract class rather than an interface.

How to store unique key value pair in Java? ›

Java HashMap class implements the Map interface which allows us to store key and value pair, where keys should be unique. If you try to insert the duplicate key, it will replace the element of the corresponding key. It is easy to perform operations using the key index like updation, deletion, etc.

Which data structure stores unique values in Java? ›

A Set is a built-in data structure that stores an unordered collection of unique values. A Set can only store reference type values.

What is a data structure with unique keys and values? ›

HashMap is a data structure that uses the Map interface and a hash table for storing key-value pairs. It's a widely used data structure in Java that provides efficient access and manipulation of data based on unique keys.

Is A HashMap just an array? ›

Hash map is implemented as an array, in which every element includes a list. The lists contain (key, value) pairs. The user can search from the hash map based on the key, and they can also add new key-value pairs into it. Each key can appear at most once in the hash map.

Why not use a HashMap for everything? ›

HashMap has significant memory footprint so there are some use cases where you memory is too precious than time complexity then you HashMap may not be the best choice. HashMap is not an answer for range queries or prefix queries.

How do you find the key of a value on a map? ›

Example: Get key for a given value in HashMap

Here, the entrySet() method returns a set view of all the entries. Inside the if statement we check if the value from the entry is the same as the given value. And, for matching value, we get the corresponding key.

Can we use a map without a key or symbols? ›

A map key is a little box of information found at the bottom of a map. It contains an explanation of what the different lines and symbols on the map mean, as well as a scale for you to work out distance. Without a map key, it would be very, very difficult to understand a map.

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

The . containsValue() method determines whether a map contains a given value. This method returns true if the map has one or more keys that match with the given value and false if the map does not contain the given value.

What is the set of key-value pairs? ›

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

How do you get the first key-value pair of a Map? ›

To find the first key-value pair in a C++ map, we can use the std::map::begin() function that returns an iterator pointing to the first element in the map. We can dereference this iterator to access the key and value or we can also use the arrow operator(->) to directly access the key and value.

Can you change the value of a key in a Map? ›

The combination of containsKey and put methods is another way to update the value of a key in HashMap. This option checks if the map already contains a key. In such a case, we can update the value using the put method. Otherwise, we can either add an entry to the map or do nothing.

How to set values in Map in Java? ›

We can insert values with a unique key and get those values back using those unique keys. To insert data in a HashMap, you can use the Java HashMap put() method. It takes in two parameters: the unique key and the value to be inserted, and it adds them to your Map.

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Lidia Grady

Last Updated:

Views: 5435

Rating: 4.4 / 5 (65 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Lidia Grady

Birthday: 1992-01-22

Address: Suite 493 356 Dale Fall, New Wanda, RI 52485

Phone: +29914464387516

Job: Customer Engineer

Hobby: Cryptography, Writing, Dowsing, Stand-up comedy, Calligraphy, Web surfing, Ghost hunting

Introduction: My name is Lidia Grady, I am a thankful, fine, glamorous, lucky, lively, pleasant, shiny person who loves writing and wants to share my knowledge and understanding with you.