https://github.com/jimschubert/ordered-map
An ordered map is a go data structure which retains key insertion order
https://github.com/jimschubert/ordered-map
go golang
Last synced: over 1 year ago
JSON representation
An ordered map is a go data structure which retains key insertion order
- Host: GitHub
- URL: https://github.com/jimschubert/ordered-map
- Owner: jimschubert
- License: apache-2.0
- Created: 2023-02-05T20:22:42.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-02-07T03:54:08.000Z (over 3 years ago)
- Last Synced: 2025-02-15T05:46:20.377Z (over 1 year ago)
- Topics: go, golang
- Language: Go
- Homepage: https://pkg.go.dev/github.com/jimschubert/ordered-map
- Size: 34.2 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# ordered-map
[](./LICENSE)

[](https://github.com/jimschubert/ordered-map/actions/workflows/build.yml)
[](https://goreportcard.com/report/github.com/jimschubert/ordered-map)
Package `orderedmap` defines a generic Ordered Map data structure with an API similar to that of [container/list](https://pkg.go.dev/container/list).
The intent of an ordered map is to retain a map's key insertion order, similar to [LinkedHashMap](https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html) from Java.
It is *not* to constrain a map to *sorted* order.
## Usage
Go does not allow for extensions to built-in maps. As such there is no "range" or keyed assignments.
Construct a map with contents as follows:
```go
myMap := orderedmap.New[string, string].
Set("First", "1st").
Set("Second", "2nd").
Set("Third", "3rd")
```
Iterate the map with the provided iterator:
```go
it := myMap.Iterator()
for i := it.Next(); i != nil; i = it.Next() {
fmt.Printf("Shorthand for %q is %q.\n", i.Key, i.Value)
}
```
Or, with lengthier for loops:
```go
it := myMap.Iterator()
var i *orderedmap.KeyValuePair[string, string]
for {
i = it.Next()
if i == nil {
break
}
fmt.Printf("Shorthand for %q is %q.\n", i.Key, i.Value)
}
````
Get the value defined at some key:
```go
myValue, ok := myMap.Get("Second")
```
There are also some utility functions:
```go
myValue:= myMap.GetOrDefault("Tenth", "10th")
first := myMap.First()
last := myMap.Last()
```
There are functions to manipulate the map as well (including the order of elements):
```go
removed, ok := myMap.Remove("First")
err := myMap.MoveToFront("Third")
err := myMap.MoveToBack("Third")
err := myMap.MoveAfter("Third", "Second")
err := myMap.MoveBefore("Third", "Fourth")
err := myMap.InsertAfter("Third", "3rd", "Second")
err := myMap.InsertBefore("Third", "3rd", "Fourth")
```
# Install
```
go get -u github.com/jimschubert/ordered-map
```
# License
This project is [licensed](./LICENSE) under Apache 2.0.