Setting own class as key in java Hashmap

后端 未结 8 1947
萌比男神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.

    0 讨论(0)
  • 2020-12-09 11:27

    HashMap doesn't check compareTo();

    HashMap checks hashCode() and equals().

    0 讨论(0)
提交回复
热议问题