---
title: "Dictionaries"
url: https://docs.zeroc.com/ice/3.8/slice/user-defined-types/dictionaries
language: java
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: Java. 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), [JavaScript](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/dictionaries/js.md), [MATLAB](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/dictionaries/matlab.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/java.md) types (`short`, `int`, `long`)
- [bool](https://docs.zeroc.com/ice/3.8/slice/basic-types/java.md)
- [byte](https://docs.zeroc.com/ice/3.8/slice/basic-types/java.md)
- [string](https://docs.zeroc.com/ice/3.8/slice/basic-types/java.md)
- [enum](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/enumerations/java.md)
- [Structures](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/structures/java.md) containing only fields of legal key types

## Language Mapping

### Default Mapping

Here is the definition of our EmployeeMap once more:

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

As for sequences, the Java mapping does not create a separate named type for this definition. A Slice dictionary maps to the interface `java.util.Map<K, V>`, where `K` is the mapping of the key type and `V` is the mapping of the value type: `EmployeeMap` maps to `java.util.Map<Long, Employee>`. When Ice unmarshals a dictionary, it creates a `java.util.HashMap`. The following code demonstrates how to allocate and use an instance of `EmployeeMap`:

```java
var em = new java.util.HashMap<Long, Employee>();

Employee e = new Employee();
e.number = 31;
e.firstName = "James";
e.lastName = "Gosling";

em.put(e.number, e);
```

### Custom Mapping for Dictionaries

If the semantics of a `HashMap` are not suitable for your application, you can specify another instance type with the `java:type` metadata directive:

```slice
["java:type:java.util.TreeMap<String, String>"]
dictionary<string, string> StringMap;
```

Ice then creates a `java.util.TreeMap<String, String>` when it unmarshals a `StringMap`, and the generated code keeps using `java.util.Map<String, String>`, the default formal type, everywhere else. A formal type you specify must implement `java.util.Map<K, V>`.

The instance type must have an accessible parameterless constructor and must be modifiable: when unmarshaling a dictionary, the generated code creates the instance with this constructor and fills it with `put`.

The type parameters (`String` above) are the Java mappings of the key and value types. See [Sequences](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/sequences/java.md) for the full syntax of `java:type`.
