Java 8: Formatting lambda with newlines and indentation

后端 未结 9 1121
[愿得一人]
[愿得一人] 2020-12-12 23:46

What I would like to achieve with lambda indentation is the following:

Multi-line statement:

String[] ppl = new String[] { \"Karen (F)\", \"Kevin (M)         


        
相关标签:
9条回答
  • 2020-12-13 00:02

    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());
    
    0 讨论(0)
  • 2020-12-13 00:07

    Latest version of Eclipse has a built-in formatter java 8 formatting, goto

    Preferences -> Java -> Code Style -> Formatter -> Active profile
    
    and Select Eclipse 2.1[built-in] profile
    
    0 讨论(0)
  • 2020-12-13 00:09

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

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

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

    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
    
    0 讨论(0)
提交回复
热议问题