An open API service indexing awesome lists of open source software.

https://github.com/bibenga/orderedmap

OrderedMap provides three implementations of a map that preserves insertion order.
https://github.com/bibenga/orderedmap

golang hashmap linkedhashmap

Last synced: 15 days ago
JSON representation

OrderedMap provides three implementations of a map that preserves insertion order.

Awesome Lists containing this project

README

          

# OrderedMap for Go

**OrderedMap** provides three implementations of a map that preserves **insertion order**.
Unlike Go’s built-in `map`, these maps maintain predictable iteration order.

This library built on top of [AbstractLinkedMap](https://github.com/apache/commons-collections/blob/master/src/main/java/org/apache/commons/collections4/map/AbstractLinkedMap.java) from Apache Commons Collections.

## Available Implementations

### 1. `IntLinkedHashMap[K gomaps.Integer, V any]`
- Key type: any integers
- Optimized for integer keys
- Preserves insertion order
- Fast `Get`, `Put`, `Delete`, and iteration
- Predictable iteration via `Keys()`, `Values()`, and `Items()`

### 2. `LinkedHashMap[K comparable, V any]`
- Generic key type
- Requires a **custom hash function** for the key type
- Preserves insertion order
- Useful for complex key types (structs, strings, etc.)
- Predictable iteration via `Keys()`, `Values()`, and `Items()`

### 3. `LinkedMapEntry[K comparable, V any]`
- Wrapper over Go’s built-in `map[K]V`
- Uses `container/list` to maintain insertion order
- Simple, safe, and fully generic
- Slightly higher overhead due to linked list
- Predictable iteration via `Keys()`, `Values()`, and `Items()`

## Installation
```bash
go get github.com/bibenga/orderedmap
```

## Usage
```go
import "github.com/olala/orderedmap/intlinkedhashmap"

// IntLinkedHashMap
intMap := intlinkedhashmap.NewDefault[int, int]()
intMap.Put(1, 12)
intMap.Put(2, 234)
for k, v := range m.Items() {
...
}
```