Skip to content

Add proposal to convert a loop to a forEach method #2934

@nlisker

Description

@nlisker

Given

List<String> list = List.of("a");
for (String adj : list) {
    System.out.println(adj);
}

Highlighting the loop and pressing ctrl+1 gives the options:
Image

I propose to add an option to convert to

list.forEach(s -> System.out.println(s));

Note that the forEach argument can be an expression, a block, and sometimes a method reference. I don't know if all valid options should be presented, just a block (always valid), or attempt to use an expression and if that's not possible then a block, or something else. In any case, another transformation on the result (ctrl+1) can convert between them.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions