我今天有一个面试,我得到了下面的Java代码:HashMap的Java中
public class Question_6 {
public static void main(String[] args){
Map<Integer,String> map1 = new HashMap<Integer,String>();
map1.put(new Integer(1),"001a");
map1.put(new Integer(1),"001b");
map1.put(new Integer(2),"002");
System.out.println(map1.size());
Map<MyInt,String> map2 = new HashMap<MyInt,String>();
map2.put(new MyInt(1),"001a");
map2.put(new MyInt(1),"001b");
map2.put(new MyInt(2),"002");
System.out.println(map2.size());
}
}
public class MyInt {
int i;
public MyInt(int i) {
this.i = i;
}
}
的问题是:
什么将被打印到控制台?
建议问题的解决方案。
我现在知道答案的第一个问题是:
2
3
但我不知道为什么? MyInt
有什么问题?
'的hashCode()'和'等于()'实现失踪。 –