MrWasdennnoch
MrWasdennnoch

Reputation: 156

IntelliJ IDEA weird foreach-loop warning

IntelliJ IDEA is giving me a weird "weak warning" in my code where I loop through an ArrayList:

Weird warning

From what I know this is already a foreach-loop, so I'm confused why IDEA gives me this warning. I'm assuming that this is a bug, what do you think?

When I click "more" it shows the following text -

This inspection reports foreach loops which can be replaced with stream api calls. Stream api is not available under Java 1.7 or earlier JVMs.

Upvotes: 3

Views: 3815

Answers (2)

Shannon S
Shannon S

Reputation: 191

Using Lambda expressions, you can streamline your code.

Collection<String> platforms = Collections.EMPTY_LIST;
platforms.add("Platform A");
platforms.add("Platform B");

// Prior to Java 8:
for (String platform : platforms)
    System.out.println(platform);

// With Java 8 Lambda expressions:
platforms.forEach((platform) -> System.out.println(platform));

Upvotes: 3

Bas Leijdekkers
Bas Leijdekkers

Reputation: 26462

IntelliJ IDEA wants to replace the loop with a Java 8 stream api forEach() call (using a lambda). I think that warning is a little unclear, but it's from the Java | Java language level migration aids | foreach loop can be collapsed with stream api inspection.

Upvotes: 4

Related Questions