Skip to content
Advertisement

Java: can’t use Map.Entry to iterate over a Map?

Every Java Map iteration example I’ve seen recommends this paradigm:

JavaScript

However, when I attempt to do this I get a warning from my compiler:

JavaScript

Here’s my code – the only wrinkle I see is that I’m iterating over an array of Map objects, and then iterating over the elements of the individual Map:

JavaScript

Am I missing something blatantly obvious?

Advertisement

Answer

This is happening because you are using raw types: a List<LinkedHashMap> instead of a List<LinkedHashMap<Something, SomethingElse>>. As a result, the entrySet is just a Set instead of a Set<Map.Entry<Something, SomethingElse>>. Don’t do that.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement