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

前端 未结 4 681
自闭症患者
自闭症患者 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:19

    A byte[] (or any array) can't work properly as a key in a HashMap, since arrays don't override equals, so two arrays will be considered equal only if they refer to the same object.

    You'll have to wrap your byte[] in some custom class that overrides hashCode and equals, and use that custom class as the key to your HashMap.

提交回复
热议问题