简体   繁体   中英

“incompatible types” in foreach with jdk 1.7 - no error with 1.6

I have code similar to the following which compiles with jdk 1.6.0_22 but not with jdk 1.7.0_79:

for(Entry<A, B> entry: aBean.getData().entrySet()) { }

getData() returns a Map<A, B> . With 1.7 the following compile error occurs, suggesting that the generics of the returned Set are erased:

[compile] /path/to/file.java:170: error: incompatible types
[compile] for (Entry<A, B> entry: aBean.getData().entrySet()) {
[compile] ^
[compile] required: Entry<A, B>
[compile] found: Object 

The error disappears when I use a local variable to hold the Set:

Set<Entry<A, B>> mySet = aBean.getData().entrySet();
for(Entry<A, B> entry: mySet) { } //works

Given that this compiles under 1.6, am I correct in assuming this is a compile Bug? If not, what am I doing wrong?

Have you already tested this with Java 8? If the issue still exists with the latest – and only supported – Java version, I suggest you create a bug report .

Otherwise use the solution you already provided in your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM