# NativeHashMap<TKey, TValue>

> An unordered, expandable associative array.

## Definition

* **Type:** Struct
* **Namespace:** [Unity.Collections](/engine/6000.7/script-reference/unity/collections.md)
* **Assembly:** UnityEngine.ManagedKernelModule
* **Implements:** [INativeDisposable](/engine/6000.7/script-reference/unity/collections/inativedisposable.md), [IDisposable](https://learn.microsoft.com/dotnet/api/system.idisposable), [IEnumerable\<KVPair\<T, T>>](https://learn.microsoft.com/dotnet/api/system.collections.generic.ienumerable-1), [IEnumerable](https://learn.microsoft.com/dotnet/api/system.collections.ienumerable)

```csharp
public struct NativeHashMap<TKey, TValue> : INativeDisposable, IDisposable, IEnumerable<KVPair<TKey, TValue>>, IEnumerable where TKey : unmanaged, IEquatable<TKey> where TValue : unmanaged
```

## Remarks

Not suitable for parallel write access. Use [NativeParallelHashMap\<TKey, TValue>](/engine/6000.7/script-reference/unity/collections/nativeparallelhashmap2.md) instead.

## Static Fields

| Value                                                                                          | Description                                                     |
| ---------------------------------------------------------------------------------------------- | --------------------------------------------------------------- |
| [MaxCapacity](/engine/6000.7/script-reference/unity/collections/nativehashmap2/maxcapacity.md) | The maximum number of elements this type of container can hold. |

## Constructors

| Constructor                                                                                                                                                   | Description                                           |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------- |
| [NativeHashMap\`2(System.Int32,Unity.Collections.AllocatorManager.AllocatorHandle)](/engine/6000.7/script-reference/unity/collections/nativehashmap2/ctor.md) | Initializes and returns an instance of UnsafeHashMap. |

## Properties

| Property                                                                                   | Description                                                         |
| ------------------------------------------------------------------------------------------ | ------------------------------------------------------------------- |
| [Capacity](/engine/6000.7/script-reference/unity/collections/nativehashmap2/capacity.md)   | The number of key-value pairs that fit in the current allocation.   |
| [Count](/engine/6000.7/script-reference/unity/collections/nativehashmap2/count.md)         | The current number of key-value pairs in this hash map.             |
| [IsCreated](/engine/6000.7/script-reference/unity/collections/nativehashmap2/iscreated.md) | Whether this hash map has been allocated (and not yet deallocated). |
| [IsEmpty](/engine/6000.7/script-reference/unity/collections/nativehashmap2/isempty.md)     | Whether this hash map is empty.                                     |
| [this\[\]](/engine/6000.7/script-reference/unity/collections/nativehashmap2/item.md)       | Gets and sets values by key.                                        |

## Methods

| Method                                                                                                     | Description                                                                                             |
| ---------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------- |
| [Add](/engine/6000.7/script-reference/unity/collections/nativehashmap2/add.md)                             | Adds a new key-value pair.                                                                              |
| [AsReadOnly](/engine/6000.7/script-reference/unity/collections/nativehashmap2/asreadonly.md)               | Returns a readonly version of this NativeHashMap instance.                                              |
| [Clear](/engine/6000.7/script-reference/unity/collections/nativehashmap2/clear.md)                         | Removes all key-value pairs.                                                                            |
| [ContainsKey](/engine/6000.7/script-reference/unity/collections/nativehashmap2/containskey.md)             | Returns true if a given key is present in this hash map.                                                |
| [Dispose](/engine/6000.7/script-reference/unity/collections/nativehashmap2/dispose.md)                     | Releases all resources (memory).                                                                        |
| [GetEnumerator](/engine/6000.7/script-reference/unity/collections/nativehashmap2/getenumerator.md)         | Returns an enumerator over the key-value pairs of this hash map.                                        |
| [GetKeyArray](/engine/6000.7/script-reference/unity/collections/nativehashmap2/getkeyarray.md)             | Returns an array with a copy of all this hash map's keys (in no particular order).                      |
| [GetKeyValueArrays](/engine/6000.7/script-reference/unity/collections/nativehashmap2/getkeyvaluearrays.md) | Returns a NativeKeyValueArrays with a copy of all this hash map's keys and values.                      |
| [GetValueArray](/engine/6000.7/script-reference/unity/collections/nativehashmap2/getvaluearray.md)         | Returns an array with a copy of all this hash map's values (in no particular order).                    |
| [Remove](/engine/6000.7/script-reference/unity/collections/nativehashmap2/remove.md)                       | Removes a key-value pair.                                                                               |
| [TrimExcess](/engine/6000.7/script-reference/unity/collections/nativehashmap2/trimexcess.md)               | Sets the capacity to match what it would be if it had been originally initialized with all its entries. |
| [TryAdd](/engine/6000.7/script-reference/unity/collections/nativehashmap2/tryadd.md)                       | Adds a new key-value pair.                                                                              |
| [TryGetValue](/engine/6000.7/script-reference/unity/collections/nativehashmap2/trygetvalue.md)             | Returns the value associated with a key.                                                                |

## Structs

| Struct                                                                                                                    | Description                                                                                  |
| ------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------- |
| [NativeHashMap\<TKey, TValue>.Enumerator](/engine/6000.7/script-reference/unity/collections/nativehashmap2/enumerator.md) | An enumerator over the key-value pairs of a container.                                       |
| [NativeHashMap\<TKey, TValue>.ReadOnly](/engine/6000.7/script-reference/unity/collections/nativehashmap2/readonly.md)     | A read-only alias for the value of a NativeHashMap. Does not have its own allocated storage. |
