HashMap with byte array key and String value - containsKey() function doesn't work

前端 未结 4 689
自闭症患者
自闭症患者 2021-01-18 00:53

I\'m using a HashMap: byte[] key and String value. But I realize that even I put the same object (same byte array and same string value) by using

myList.put(         


        
4条回答
  •  爱一瞬间的悲伤
    2021-01-18 01:41

    Adding to Eran's clear answer,Since byte[] or any array doesnt override hashcode and equals(it uses the default methods of Object class ),you can always wrap around a String Object which takes byte[] as constructor argument.Not only does String form good keys in Map,they are immutable too(the operations in a Hash based map are faster)

    http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#String(byte[])

提交回复
热议问题