28

I'm using IntelliJ IDEA 13 with Java 8 and wonder how to autocomplete lambdas. Before Java 8 I used anonymous inner classes of course. I auto completed by typing "new" and hitting Ctrl+Space:

autocomplete new

and choosing the first option.

Now with Java 8 I want to generate lambdas as well, inferring parameters and all, but I can't find autocompletion for it.

Note, this above example is quite easy, but when you have multiple parameters with generic types, e.g. JavaFX Listeners and the like, autocompletion comes in handy.

Tim Büthe
  • 62,884
  • 17
  • 92
  • 129
  • Is typing `t->` manually really *that* hard? The fact that you can omit the type of the parameter applies to generic types as well. – Holger Aug 07 '14 at 13:36
  • I used an easy example, a more realistic one would be this: `comboBox.focusedProperty().addListener((observable, oldValue, newValue) -> ...);` – Tim Büthe Aug 07 '14 at 14:27
  • If you insist on using such long parameter names I see your point. However, I’m wondering why you are using lambda expressions then… – Holger Aug 07 '14 at 14:45

2 Answers2

37

In Windows or Linux, it’s Ctrl+Space

In Mac OS, it’s Ctrl+Shift+Space

enter image description here

leventov
  • 14,760
  • 11
  • 69
  • 98
Tim Büthe
  • 62,884
  • 17
  • 92
  • 129
7

You can just type p = s -> f..., autocompletion will then suggest false. If you're not sure about lambda, you can start with the inner class, and then turn it into a lambda expression.

In your example you can press Alt+Enter afterwards, as follows:

After completing:

After completing

After pressing Alt+Enter:

After pressing Alt+Enter

Result:

Result

Clicking the small icon at the gutter shows you the Predicate.test method. Override icon at gutter

sina72
  • 4,931
  • 3
  • 35
  • 36
  • This is exactly what I do right now, but I have to move the cursor to that grayed out part before I can hit Alt+Enter, right? However, I believe this is too complicated and IDEA should have / does have some other shortcut for that. – Tim Büthe Aug 07 '14 at 13:11
  • True, I am doing the same. Sorry, I am only aware of these intentions and this little "Override" icon at the gutter, which shows you the overriden Predicate.test method. – sina72 Aug 07 '14 at 13:27
  • type `p = s -> f...`? is this a joke? – user924 Aug 28 '18 at 08:31