Search code examples
intellij-idealambdaautocomplete

Auto-generate lambda expression as argument in IntelliJ


How do I get IntelliJ to auto-generate a lambda expression as the argument being passed?

enter image description here

What I want:

enter image description here

I have seen the Question How to autocomplete lambdas in IntelliJ IDEA?, but that does not seem to produce my desired result.


Solution

  • As Tagir Valeev already mentioned in his comment, IntelliJ will auto-generate lambda code, but only if your ClickListener is a (functional) interface.

    E.g.:

    @FunctionalInterface
    interface ClickListener {
      void listen();
    }
    
    class Clicker {
       void addClickListener(ClickListener listener){
          // ...
       }
    }
    

    Then, Intellij suggests upon hitting Ctrl+Shift+Space

    enter image description here

    Note: As to the @FunctionalInterface annotation, Jav Doc says

    This annotation is not a requirement for the compiler to recognize an interface as a functional interface