"Incompatible types" when accessing Map#entrySet()
Here's a demonstration of what you may be doing - it is difficult to be sure without more code.
class ATest<T> {
Map<String, String> props = new HashMap<String, String>();
void aTest() {
// Works fine.
for (Map.Entry<String, String> entry : props.entrySet()) {
}
}
void bTest() {
ATest aTest = new ATest();
// ERROR! incompatible types: Object cannot be converted to Entry<String,String>
for (Map.Entry<String, String> entry : aTest.props.entrySet()) {
}
}
void cTest(Map props) {
// ERROR! incompatible types: Object cannot be converted to Entry<String,String>
for (Map.Entry<String, String> entry : props.entrySet()) {
}
}
}
Notice that in bTest
I create an ATest
without its generic type parameter. In that situation Java removes all generic information from the class, including, as you will see, the <String,String>
from the props
variable inside it.
Alternatively - you may be accidentally removing the generic nature of the properties map - like I demonstrate in cTest
.