Setting own class as key in java Hashmap

后端 未结 8 1964
萌比男神i
萌比男神i 2020-12-09 10:24

I have a class which I want to set up as keys in HashMap. I already have implemented the compareTo method for that class. But still when I do:

map.put(new MyKey(d         


        
8条回答
  •  鱼传尺愫
    2020-12-09 11:21

    You need to implement hashCode() and equals(). compareTo() is additionally required for sorted map/set.

    See this question for details.

提交回复
热议问题