---
title: "Dictionaries"
url: https://docs.zeroc.com/ice/3.8/slice/user-defined-types/dictionaries
language: matlab
docs_index: https://docs.zeroc.com/llms.txt
---

> For an index of every page, see [llms.txt](https://docs.zeroc.com/llms.txt).

# Dictionaries

Programming language: MATLAB. Other languages: [C++](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/dictionaries/cpp.md), [C#](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/dictionaries/csharp.md), [Java](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/dictionaries/java.md), [JavaScript](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/dictionaries/js.md), [PHP](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/dictionaries/php.md), [Python](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/dictionaries/python.md), [Ruby](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/dictionaries/ruby.md), [Swift](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/dictionaries/swift.md).

## Dictionary Syntax and Semantics

A dictionary is an associative array: a collection of key-value pairs in which each key is unique.

For example:

```slice
module M
{
    struct Employee
    {
        long   number;
        string firstName;
        string lastName;
    }

    dictionary<long, Employee> EmployeeMap;
}
```

This definition creates a dictionary named `EmployeeMap` that maps from an employee number to a structure containing the details for an employee.

## Allowable Types for Dictionary Keys and Values

The value type of a dictionary can be any Slice type. However, the key type of a dictionary is limited to one of the following types:

- [Integral](https://docs.zeroc.com/ice/3.8/slice/basic-types/matlab.md) types (`short`, `int`, `long`)
- [bool](https://docs.zeroc.com/ice/3.8/slice/basic-types/matlab.md)
- [byte](https://docs.zeroc.com/ice/3.8/slice/basic-types/matlab.md)
- [string](https://docs.zeroc.com/ice/3.8/slice/basic-types/matlab.md)
- [enum](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/enumerations/matlab.md)
- [Structures](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/structures/matlab.md) containing only fields of legal key types

## Language Mapping

A Slice dictionary maps to a MATLAB dictionary.

The key type of the MATLAB dictionary is the mapped type for the Slice dictionary key. For example, a Slice `string` key maps to a MATLAB `char` key, which MATLAB interprets as a `string` type.

The value type of the MATLAB dictionary depends on the Slice value type:

| **Slice Value Type** | **MATLAB Value Type** |
| --- | --- |
| `bool`, numeric type, `enum`, `struct` | Corresponding MATLAB type |
| `string` | MATLAB `string` |
| `class`, proxy, `sequence`, `dictionary` | Cell of the corresponding MATLAB type |

Consider the definition of our `EmployeeMap` once more:

```slice
struct Employee
{
    ["matlab:identifier:Number"]
    long number;

    ["matlab:identifier:FirstName"]
    string firstName;

    ["matlab:identifier:LastName"]
    string lastName;
}

dictionary<long, Employee> EmployeeMap;
```

`EmployeeMap` maps to a dictionary with key type = `int64` and value type = `Employee` (a MATLAB class mapped from a Slice struct).

```matlab
em = configureDictionary('int64', 'M.Employee');

e = M.Employee();
e.Number = 31;
e.FirstName = 'James';
e.LastName = 'Gosling';

em(e.Number) = e;
```
