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.
- Host: GitHub
- URL: https://github.com/bibenga/orderedmap
- Owner: bibenga
- License: apache-2.0
- Created: 2025-08-12T16:18:49.000Z (6 months ago)
- Default Branch: main
- Last Pushed: 2025-08-23T16:26:43.000Z (6 months ago)
- Last Synced: 2025-08-24T06:15:49.912Z (6 months ago)
- Topics: golang, hashmap, linkedhashmap
- Language: Go
- Homepage:
- Size: 51.8 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
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() {
...
}
```