自己写代码时,需要使用自定义的类作为HashMap的Key;但是通过put操作放入到HashMap中的数据却无法被取出
eg:
MyKey mykey = new Mykey();
hashmap.put(mykey, myvalue);
MyValue result = hashmap.get(mykey)
result却一直为null。
我记得应该要在类MyKey中重载hashcode方法,但是还是没有用。
最后参照这个帖子
using two objects as a hashmap key
帖子中提到需要重载hashcode,equals方法,如果使用的是SortedMap,那么MyKey这个类还要实现Comparable接口