Why is the following code failing with compilation error?
class SampleClass{
}
public class DemoHashMap {
public static void main(String[] args) {
SampleClass s1 = new SampleClass();
SampleClass s2 = new SampleClass();
Map<Object, Integer> counts = new HashMap<Object, Integer>();
counts.add(s1, 1);
counts.add(s2, 2);
}
}
This code is not allowing me to add s1 & s2 to the hashmap. In the declaration for counts I have specified that the key can be Object.