首页 > 其他分享 >golang map key struct hash policy

golang map key struct hash policy

时间:2023-05-08 20:35:24浏览次数:45  
标签:map use hash struct Key key type

  The easiest and most flexible way is to use a struct as the key type, including all the data you want to be part of the key, so in your case:

type Key struct {
    X, Y int
}

And that's all. Using it:

m := map[Key]int{}
m[Key{2, 2}] = 4
m[Key{2, 3}] = 8

fmt.Println("2^2 = ", m[Key{2, 2}])
fmt.Println("2^3 = ", m[Key{2, 3}])

Output (try it on the Go Playground):

2^2 =  4
2^3 =  8

Spec: Map types: You may use any types as the key where the comparison operators == and != are fully defined, and the above Key struct type fulfills this.

Spec: Comparison operators: Struct values are comparable if all their fields are comparable. Two struct values are equal if their corresponding non-blank fields are equal.

One important thing: you should not use a pointer as the key type (e.g. *Key), because comparing pointers only compares the memory address, and not the pointed values.

Also note that you could also use arrays (not slices) as key type, but arrays are not as flexible as structs. You can read more about this here: Why have arrays in Go?

This is how it would look like with arrays:

type Key [2]int

m := map[Key]int{}
m[Key{2, 2}] = 4
m[Key{2, 3}] = 8

fmt.Println("2^2 = ", m[Key{2, 2}])
fmt.Println("2^3 = ", m[Key{2, 3}])

Output is the same. Try it on the Go Playground.

 

出处:https://stackoverflow.com/questions/52348514/how-to-make-composite-key-for-a-hash-map-in-go

标签:map,use,hash,struct,Key,key,type
From: https://www.cnblogs.com/leodaxin/p/17383017.html

相关文章

  • map和multimap
    map相对于set区别,map具有键值和实值,所有元素根据键值自动排序,pair的第一个值被称为键值key,pair的第二个值被称为实值value。map也是以红黑树为底层实现机制,根据key进行排序构造函数#include<map>map<int,string>m;multimap<T1,T2>mlmap;插入方法一map的key重复将无法......
  • 关于map或for自动停止循环的问题
    问题在维护一个老项目时发现map循环数组循坏到index为14时就自动不循环了(数组长度为79),并且也不运行后面的代码代码//问题代码awaituseMyFetch('url',{afterFetch(ctx){constresdata=safeParse(ctx.data)//json字符串转换为对象co......
  • asyncio.Semaphore
     asyncio.Semaphore是一个异步信号量,用于协调多个协程对共享资源的访问。异步信号量在协程中的使用方式与线程中的普通信号量类似,但是它是非阻塞的。当信号量的计数器为0时,协程将会被阻塞,直到其他协程释放了该信号量。 importasyncioasyncdefworker(semaphore):......
  • 如何决定使用 HashMap 还是 TreeMap?
    @[toc]问:如何决定使用HashMap还是TreeMap?介绍TreeMap<k,v>的Key值是要求实现java.lang.Comparable,所以迭代的时候TreeMap默认是按照Key值升序排序的;TreeMap的实现是基于红黑树结构。适用于按自然顺序或自定义顺序遍历键(key)。HashMap<k,v>的Key值实现散列hashCode(),分布是散列的......
  • nmap一些使用
     Nmap使用Nmap是主机扫描工具,他的图形化界面是Zenmap,分布式框架为Dnamp。Nmap可以完成以下任务:主机探测端口扫描版本检测系统检测支持探测脚本的编写Nmap在实际中应用场合如下:通过对设备或者防火墙的探测来审计它的安全性探测目标主机所开放的端口通过识别新......
  • Map
    Mapmap线程安全方式k/v为null数据结构(1.8)扩容机制迭代器HashMap不安全均可数组+链表+红黑树初始16,扩容2倍容器本身ConcurrentHashMap锁分段+CASNPE数组+链表+红黑树初始16,扩容2倍容器的克隆HashtablesynchronizedNPE数组+链表初始11,扩容2......
  • js基础---对象的序列化(JSON)与map
    序列化概念json工具类就是那个转换字符串的方法调用json静态方法,不需要new。注意事项将对象转换为json后再转换为对象,相当于做了一次深复制。当对象的字符串key属性满足不了需求时,可用map的对象属性作为keymap属性和方法map与数组之间的转换......
  • Mybatis-Plus基本CRUD——通用Mapper
    BaseMapper接口MyBatis-Plus中的基本CRUD在内置的BaseMapper中都已得到了实现,我们可以直接使用,接口如下:/***Mapper继承该接口后,无需编写mapper.xml文件,即可获得CRUD功能*<p>这个Mapper支持id泛型</p>**@authorhubin*@since2016-01-23*/publicinter......
  • springboot集成下,mybatis的mapper代理对象究竟是如何生成的
    springboot集成下,mybatis的mapper代理对象究竟是如何生成的 前情回顾Mybatis源码解析-mapper代理对象的生成,你有想过吗,我们讲到了mybatis操作数据库的流程:先创建SqlSessionFactory,然后创建SqlSession,然后再创建获取mapper代理对象,最后利用mapper代理对象完成数据库......
  • hashmap oop in golang
    packagemainimport("fmt")constHASH_BUCKET_SIZE=3//1023typehash_nodestruct{keyinterface{}valinterface{}next*hash_node}typeHASH_BUCKET[HASH_BUCKET_SIZE]*hash_nodefunchash(keyinterface{})int{h......