I have a HashMap implemented as:
Map<Integer, ArrayList<Integer>> hm = new HashMap<Integer, ArrayList<Integer>>();
After the following operations:
hm.put((Integer) 1, new ArrayList<Integer>());
hm.put((Integer) 2, new ArrayList<Integer>());
(hm.get(1)).add(2);
(hm.get(1)).add(2);
(hm.get(1)).add(3);
(hm.get(2)).add(4);
I get my Map as:
1: [2,2,3]
2: [4]
Now, I want to remove all the occurences of 2 from key 1, i.e., I want to modify my HashMap so that it looks like:
1: [3]
2: [4]
I did the following:
for(List<Integer> list : (hm.get(1)))
{
list.removeAll(Collections.singleton(2));
}
However, while compilation, this error shows up:
error: incompatible types
for(List<Integer> list : hm.get(1))
^
required: List<Integer>
found: Integer
1 error
However, when I run:
System.out.println((hm.get(1)).getClass());
I get:
class java.util.ArrayList
According to which, I think, my code is fine (Even after applying casts, this error shows up in another form).
I don't know as to why this is happening. What am I doing wrong? How to fix this?