orderedmap

package module
v0.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 23, 2025 License: Apache-2.0 Imports: 1 Imported by: 0

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

Documentation

Overview

This implementation is inspired by Apache Commons Collections (licensed under the Apache License, Version 2.0).

Index

Constants

View Source
const DefaultCapacity = 16

DefaultCapacity is the initial capacity of the map when no specific size is provided. Typical default is 16.

View Source
const DefaultLoadFactor = 0.75

DefaultLoadFactor determines when the map should grow (rehash). A load factor of 0.75 means the map will resize when 75% full.

View Source
const MaxCapacity = 1 << 30

MaxCapacity defines the maximum number of elements the map can hold. It is set to 2^30 to prevent excessive memory usage.

Variables

This section is empty.

Functions

This section is empty.

Types

type Integer

type Integer interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
}

Integer is a type constraint for all built-in integer types. It includes both signed and unsigned integers of all sizes.

type Map

type Map[K, V any] interface {
	// Size returns the number of elements in the map.
	Size() int

	// IsEmpty returns true if the map contains no elements.
	IsEmpty() bool

	// Clear removes all elements from the map.
	Clear()

	// Clone creates a shallow copy of the map.
	Clone() Map[K, V]

	// Get returns the value associated with the given key
	// and a boolean indicating if the key exists.
	Get(K) (V, bool)

	// Put inserts or updates the value for a key. It returns
	// the previous value and a boolean indicating if the key existed.
	Put(K, V) (V, bool)

	// Delete removes the key from the map. Returns the value
	// and a boolean indicating if the key existed.
	Delete(K) (V, bool)

	// Keys returns a sequence of keys in insertion order.
	Keys() iter.Seq[K]

	// Values returns a sequence of values in insertion order.
	Values() iter.Seq[V]

	// Items returns a sequence of key-value pairs in insertion order.
	Items() iter.Seq2[K, V]
}

Map is a generic interface for an ordered map that preserves the insertion order of keys. It supports basic map operations, iteration, and cloning.

K - key type V - value type

Directories

Path Synopsis
internal
hash
Portions Copyright 2025 bibenga
Portions Copyright 2025 bibenga
Portions of this file are based on Apache Commons Collections, licensed under the Apache License, Version 2.0.
Portions of this file are based on Apache Commons Collections, licensed under the Apache License, Version 2.0.
Portions Copyright 2025 bibenga
Portions Copyright 2025 bibenga

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL