What happened to foreach in java6?

Map testMap = new HashMap();
for(Map.Entry<String, Object> entry:testMap.entrySet()){

}

he has a hint: "Type of mismatch: cannot be converted from the type of the Object element to Map.Entry"

Could you tell me the reason?

thank

+5
source share
4 answers

testMapdoes not have a general type, so it testMap.entrySetreturns objects.

You can fix it as follows:

Map<String, Object> testMap = new HashMap<String, Object>();
for(Map.Entry<String, Object> entry:testMap.entrySet()){

}
+6
source

Maybe you should declare testMap as

Map<String, Object> testMap = new HashMap<String, Object>();
+6
source

Map testMap = new HashMap();

, testMap.entrySet() <String, Object>

Map<String, Object> testMap = new HashMap<String, Object>();

Java6.

+2

, , raw .

Map testMap = new HashMap();

Map<Object,Object> testMap = new HashMap<Object,Object>();

Map.Entry<String, Object>.

-

Map<String,Object> testMap = new HashMap<String,Object>();
0

All Articles