Java 8: Formatting lambda with newlines and indentation

In Eclipse, for the single-line statements:

In your project or global preferences, go to Java -> Code Style -> Formatter -> Edit -> Line Wrapping -> Function Calls -> Qualified Invocations, set Wrap all elements, except first if not necessary and tick Force split, even if line shorter than maximum line width.


Out of the box IntelliJ 13 will probably work for you.

If I write it this way:

// Mulit-Line Statement
String[] ppl = new String[] { "Karen (F)", "Kevin (M)", "Lee (M)", "Joan (F)", "Des (M)", "Rick (M)" };
List<String> strings = Arrays.stream(ppl)
        .filter(
                (x) ->
                {
                    return x.contains("(M)");
                }
        ).collect(Collectors.toList());
strings.stream().forEach(System.out::println);

And then apply the auto formatter (no changes):

// Mulit-Line Statement
String[] ppl = new String[]{"Karen (F)", "Kevin (M)", "Lee (M)", "Joan (F)", "Des (M)", "Rick (M)"};
List<String> strings = Arrays.stream(ppl)
        .filter(
                (x) ->
                {
                    return x.contains("(M)");
                }
        ).collect(Collectors.toList());
strings.stream().forEach(System.out::println);

The same is true for your single line statement. It has been my experience that IntelliJ is more flexible in how its auto formatting is applied. IntelliJ is less likely to remove or add line returns, if you put it there then it assumes you meant to put it there. IntelliJ will happily adjust your tab-space for you.


IntelliJ can also be configured to do some of this for you. Under "settings" -> "code style" -> "java", in the "Wrapping and Braces" tab you can set "chain method calls" to "wrap always".

Before Auto-Formatting

// Mulit-Line Statement
List<String> strings = Arrays.stream(ppl).filter((x) -> { return x.contains("(M)"); }).collect(Collectors.toList());

// Single-Line Statement
List<String> strings = Arrays.stream(ppl).map((x) -> x.toUpperCase()).filter((x) -> x.contains("(M)")).collect(Collectors.toList());

After Auto-Formatting

// Mulit-Line Statement
List<String> strings = Arrays.stream(ppl)
        .filter((x) -> {
            return x.contains("(M)");
        })
        .collect(Collectors.toList());

// Single-Line Statement
List<String> strings = Arrays.stream(ppl)
        .map((x) -> x.toUpperCase())
        .filter((x) -> x.contains("(M)"))
        .collect(Collectors.toList());

Eclipse (Mars) has an option for lambda expressions formatter.

Go to Window > Preferences > Java > Code Style > Formatter

enter image description here

Click the Edit button, go to the Braces Tag and set the Lambda Body to Next Line Indented

enter image description here

Another option is update these properties into your project settings. (yourWorkspace > yourProject > .settings > org.eclipse.jdt.core.prefs)

org.eclipse.jdt.core.formatter.insert_space_before_lambda_arrow=insert
org.eclipse.jdt.core.formatter.brace_position_for_lambda_body=next_line_shifted

This question is now old and unfortunately, the default configuration of the Eclipse formatter remains still not user-friendly to write functional code in a readable way.

I tried all things mentioned in all other answers and no one suits for most of use cases.
It may be fine for some but unpleasant for others.

I found a way that suits for me the most of the time.
I share it by thinking that it could help others.

Note that my way has a trade-off : accepting that each qualified invocation be always on a distinct line.
It is maybe the missing option in the formatter configuration : indicating the threshold in terms of invocations to wrap the line instead of using 1 invocation by default.

Here are my 2 combined tools to handle it rather correctly :

  • Customizing Eclipse formatter configuration for most of cases

  • Creating a code template with //@formatter:off ... //@formatter:on for corner cases.


Customizing Eclipse formatter configuration
The value to change are surrounded in red in the capture.

Step 1) Create your own Java Code Style Formatter

Preferences menu and in the tree, go to Java -> Code Style -> Formatter.
Click on "New" to create a new Profile (initialize it with "Java conventions").

Eclipse formatter

The two next steps have to be performed in your custom formatter profile.

Step 2) Change the indentation configuration for wrapped lines

Identation configuration

The modification allows to use whitespaces instead of tabulations.
It will matter in the next step as we configure the line wrapping policy with the column indentation option.
It will avoid indeed creates unpleasant spaces.

Step 3) Change the default indentation for wrapped lines and the line wrapping policy for qualified invocation

Wrapped line size


Here is a test formatting with code of the question.

Before formatting :

void multiLineStatements() {
    String[] ppl = new String[] { "Karen (F)", "Kevin (M)", "Lee (M)", "Joan (F)", "Des (M)", "Rick (M)" };
    List<String> strings = Arrays.stream(ppl).filter((x) ->
    {
        return x.contains("(M)");
    }).collect(Collectors.toList());
    strings.stream().forEach(System.out::println);
}

void singleLineStatements() {
    String[] ppl = new String[] { "Karen (F)", "Kevin (M)", "Lee (M)", "Joan (F)", "Des(M)", "Rick (M)" };
    List<String> strings = Arrays.stream(ppl).map((x) -> x.toUpperCase())
            .filter((x) -> x.contains("(M)")).collect(Collectors.toList());
    strings.stream().forEach(System.out::println);
}

After formatting :

void multiLineStatements() {
    String[] ppl = new String[] { "Karen (F)", "Kevin (M)", "Lee (M)", "Joan (F)", "Des (M)", "Rick (M)" };
    List<String> strings = Arrays.stream(ppl)
                                 .filter((x) -> {
                                     return x.contains("(M)");
                                 })
                                 .collect(Collectors.toList());
    strings.stream()
           .forEach(System.out::println);
}

void singleLineStatements() {
    String[] ppl = new String[] { "Karen (F)", "Kevin (M)", "Lee (M)", "Joan (F)", "Des(M)", "Rick (M)" };
    List<String> strings = Arrays.stream(ppl)
                                 .map((x) -> x.toUpperCase())
                                 .filter((x) -> x.contains("(M)"))
                                 .collect(Collectors.toList());
    strings.stream()
           .forEach(System.out::println);
}

Creating code templates with //@formatter:off ... //@formatter:on for corner cases.

Writing manually or copy-paste //@formatter:on and //@formatter:off is fine as you write it rarely.
But if you have to write it several times by week or even worse by day, a more automatic way is welcome.

Step 1) Go to Java Editor template

Preferences menu and in the tree, go to Java ->Editor -> Template.
enter image description here

Step 2) Create template to disable formatting for selected code

Template Formatter off on

You can now test it.
Select lines which you want to disable the formatting.
Now enter ctrl+space twice (first one is "Java proposals" and the second one is "template proposals").
You should get something like :

template proposal

Select the fmt template as in the screenshot and click "Enter". Done!

result after template application


I format single-line statement by adding empty comment "//" after functions.

List<Integer> list = Arrays.stream(x) //
                           .filter((n) -> n % 2 == 0) //
                           .map((n) -> n * 4) //
                           .boxed() //
                           .collect(Collectors.toList());