FULL PRODUCT VERSION :
javac 1.5.0_02
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows 2000 [Version 5.00.2195]
A DESCRIPTION OF THE PROBLEM :
With a non genericized (raw) HashMap, Map.entrySet() method return value has problem when used in an Enhanced For Loop.
If a genericized Map reference is used (e.g. Map<Object,Object>), there's no problem compiling.
The same error message output was also reproduced on SUSE 9.2 64bit with JDK 1.5.0_02 for x86_64
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The code should have compiled since the raw type of Set<Map.Entry<K,V>> should have bean Set<Map.Entry>. This should be backward compatible since it's a subclass of Set.
ERROR MESSAGES/STACK TRACES THAT OCCUR :
Warning: line (9) [unchecked] unchecked call to put(K,V) as a member of the raw type java.util.Map
Error: line (11) incompatible types
found : java.lang.Object
required: java.util.Map.Entry
NOTE: the warning message is produce only with -Xlint:unchecked option
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.util.Map;
import java.util.HashMap;
public class TestCase
{
public static void main(String[] args)
{
Map myMap = new HashMap();
myMap.put("key1","1");
for (Map.Entry e : myMap.entrySet())
System.out.println("Key = "+e.getKey() + " Value = "+e.getValue());
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Workaround 1:
Using a Temporary variable, without any casting:
Map myMap = new HashMap();
myMap.put("key1","1");
Set<Map.Entry> tempVar = myMap.entrySet();
for (Map.Entry e : tempVar)
System.out.println("Key = "+e.getKey() + " Value = "+e.getValue());
The return type is still Set instead of Set<Map.Entry>, but a warning instead of an error was produced:
Warning: line (10) [unchecked] unchecked call to put(K,V) as a member of the raw type java.util.Map
Warning: line (12) [unchecked] unchecked conversion
found : java.util.Set
required: java.util.Set<java.util.Map.Entry>
Workaround 2:
Use a genericized reference, the test case compiles without warnings:
Map<Object,Object> myMap = new HashMap<Object,Object>();
myMap.put("key1","1");
for (Map.Entry e : myMap.entrySet())
System.out.println("Key = "+e.getKey() + " Value = "+e.getValue());
###@###.### 2005-03-30 13:42:55 GMT
javac 1.5.0_02
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows 2000 [Version 5.00.2195]
A DESCRIPTION OF THE PROBLEM :
With a non genericized (raw) HashMap, Map.entrySet() method return value has problem when used in an Enhanced For Loop.
If a genericized Map reference is used (e.g. Map<Object,Object>), there's no problem compiling.
The same error message output was also reproduced on SUSE 9.2 64bit with JDK 1.5.0_02 for x86_64
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The code should have compiled since the raw type of Set<Map.Entry<K,V>> should have bean Set<Map.Entry>. This should be backward compatible since it's a subclass of Set.
ERROR MESSAGES/STACK TRACES THAT OCCUR :
Warning: line (9) [unchecked] unchecked call to put(K,V) as a member of the raw type java.util.Map
Error: line (11) incompatible types
found : java.lang.Object
required: java.util.Map.Entry
NOTE: the warning message is produce only with -Xlint:unchecked option
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.util.Map;
import java.util.HashMap;
public class TestCase
{
public static void main(String[] args)
{
Map myMap = new HashMap();
myMap.put("key1","1");
for (Map.Entry e : myMap.entrySet())
System.out.println("Key = "+e.getKey() + " Value = "+e.getValue());
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Workaround 1:
Using a Temporary variable, without any casting:
Map myMap = new HashMap();
myMap.put("key1","1");
Set<Map.Entry> tempVar = myMap.entrySet();
for (Map.Entry e : tempVar)
System.out.println("Key = "+e.getKey() + " Value = "+e.getValue());
The return type is still Set instead of Set<Map.Entry>, but a warning instead of an error was produced:
Warning: line (10) [unchecked] unchecked call to put(K,V) as a member of the raw type java.util.Map
Warning: line (12) [unchecked] unchecked conversion
found : java.util.Set
required: java.util.Set<java.util.Map.Entry>
Workaround 2:
Use a genericized reference, the test case compiles without warnings:
Map<Object,Object> myMap = new HashMap<Object,Object>();
myMap.put("key1","1");
for (Map.Entry e : myMap.entrySet())
System.out.println("Key = "+e.getKey() + " Value = "+e.getValue());
###@###.### 2005-03-30 13:42:55 GMT