Skip to content

bibenga/orderedmap

Repository files navigation

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 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

go get github.com/bibenga/orderedmap

Usage

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() {
	...
}

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages