Golang map internal implementation - how does it search the map for a key?

前端 未结 2 1100
借酒劲吻你
借酒劲吻你 2020-12-15 08:21

I\'ve read in "The Go Programming Language" that a "given key can be retrieved ... using a constant number of key comparisons on average, no matter how large

2条回答
  •  一整个雨季
    2020-12-15 09:03

    The native map type uses a hash table implementation. It uses a hashing function on the key to generate an index into an array of data. Thus, generally, most actions occur in O(1) time. This is only generally true as some keys can result in the same index when hashed, called a collision, which then must be handled specially.

    Hash tables are cool!

提交回复
热议问题