why do i get this exception?
Map myHash = null myHash = (HashMap)Collections.synchronizedMap(new HashMap());
If i try to use this hashmap, i get java.lang.ClassCastException
why do i get this exception?
Map myHash = null myHash = (HashMap)Collections.synchronizedMap(new HashMap());
If i try to use this hashmap, i get java.lang.ClassCastException
Because Collections.synchronizedMap
does not return a HashMap
.
It returns some other kind of Map
(the type of which you should not need to know), that wraps your HashMap
.
You have to use the Map
interface instead of HashMap
.