Map testMap = new HashMap();
for(Map.Entry<String, Object> entry:testMap.entrySet()){
}
it has error tip:"Type mismatch: cannot convert from element type Object to Map.Entry"
Would you tell me the reason?
thanks
testMap
is not of generic type, so testMap.entrySet
returns objects.
You can correct it like that:
Map<String, Object> testMap = new HashMap<String, Object>();
for(Map.Entry<String, Object> entry:testMap.entrySet()){
}
Maybe you should declare testMap as
Map<String, Object> testMap = new HashMap<String, Object>();
Your declaration
Map testMap = new HashMap();
does not mention that testMap.entrySet() should be type <String, Object>
The solution is
Map<String, Object> testMap = new HashMap<String, Object>();
No issues with Java6.
The problem is not with for but with the declaration of your map, you should not use raw types.
Map testMap = new HashMap();
This is more like
Map<Object,Object> testMap = new HashMap<Object,Object>();
and you are trying to cast this to Map.Entry<String, Object>
.
The solution for you is to declare properly the object
Map<String,Object> testMap = new HashMap<String,Object>();
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