Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get an arbitrary key/item from a map

Tags:

dictionary

go

I am new to Go and now I want to get an arbitrary item from a map; what's the idiomatic way to do that? I can only think of something like this:

func get_some_key(m map[int]int) int {     for k := range m {         return k     }     return 0 } 

The reason I want that is I am using a map to maintain a set of jobs, and with a map I can get a pending job or remove a finished job in O(1). I guess this should be a common requirement but it's not obvious how to do it in Go.

like image 403
chuchao333 Avatar asked May 05 '14 22:05

chuchao333


2 Answers

Whether getting an arbitrary key from a hash table is a common requirement may be discussed. Other language map implementations often lack this feature (eg. Dictionary in C# )

However, your solution is probably the fastest one, but you will be left with a pseudo-random algorithm that you do not control. And while the current implementation uses a pseudo-random algorithm, the Go Specification doesn't give you any assurance it will actually be random, only that it is not guaranteed to be predictable:

The iteration order over maps is not specified and is not guaranteed to be the same from one iteration to the next.

If you want more control of the randomization, you can also in parallel keep an updated slice of values (or keys) contained in the map, using the randomization of your choice (math/rand or crypto/rand for more extreme cases) to get the value stored at an index, selected randomly, in the slice.

like image 189
ANisus Avatar answered Sep 19 '22 09:09

ANisus


Here is a more generic version, although it may be less efficient:

    keys := reflect.ValueOf(mapI).MapKeys()     return keys[rand.Intn(len(keys))].Interface() 

https://play.golang.org/p/0uvpJ0diG4e

like image 44
Briank Avatar answered Sep 21 '22 09:09

Briank