I had an interview today, and I got the following Java code:
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;
}
}
The questions were:
What will be printed to the console?
Suggest a solution to the problem.
I know now that the answer to the first question is :
2
3
But I don't know why? What is the problem with MyInt
?
Your problem is that equals()
and hashcode()
is not implemented on MyInt
.
You are expected to have 2
as a result in both cases.
HashMap
, as the name implies, groups the keys into buckets based on the keys' hashcode(). But the default hashcode does not match for two instances of MyInt
with the same value.
To determine equality, you have to override equals()
as well.
One solution:
public class MyInt {
[...]
@Override
public int hashCode() {
return value;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof MyInt) {
return i == ((MyInt)obj).i;
}
return false;
}
}
You need to override the equals()
and hashCode()
method in your MyInt
class , so that HashMap
can comprehend new MyInt(1).equals(new MyInt(1))
is true
.
The Integer class overrides the equals()
method to do value based comparison. Hashmaps cannot contain two keys that are "equal", so the 2nd insertion into map1 will overwrite the first entry. As well, the hashcode()
method is overridden.
However, MyInt does not override the equals()
or hashcode()
method so equality is memory location based. Therefore, map2 sees three distinct keys and makes three distinct entries.
Map<MyInt,String> map2 = new HashMap<MyInt,String>();
MyInt one = new MyInt(1);
MyInt two = new MyInt(2);
map2.put(one,"001a");
map2.put(one,"001b");
map2.put(two,"002");
System.out.println(map2.size());
Produces an output of 2
in this case because one.equals(one) is true in this case.
map1.put(new Integer(1),"001a");
map1.put(new Integer(1),"001b");//same location in map
map1.put(new Integer(2),"002");
in this part you use the Integer class, Integer class don't allow setting the same location, but your Integer class allow.
Change code like this, and you see the problem
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(2),"001b");
map1.put(new Integer(3),"002");
System.out.println(map1.size());
Map<MyInt,String> map2 = new HashMap<MyInt,String>();
map2.put(new MyInt(1),"001a");
map2.put(new MyInt(2),"001b");
map2.put(new MyInt(3),"002");
System.out.println(map2.size());
}
this code will print ;
3 3
So, your Integer class(myInt) is true but missing
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With