SteelReed

Search

Search posts, questions…

Java CoreQuestion 30 / 43

Junior

What is a functional interface? When do you use a lambda versus a method reference?

Short answer

A functional interface has exactly one unimplemented (abstract) method, so a short lambda can stand in for it.

yesno: transforms, manystatementsFunctional interfaceone abstract methodLambda only callsan existing method?Method referenceUser::getNameLambda

3 points to say:

  1. Extra default and static methods are fine.
  2. Common ones: Function, Predicate, Consumer, Supplier.
  3. A lambda that only calls an existing method becomes User::getName.

Keep reading the deep dive

Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.