Social Media

Another Java Lamdba Post

The syntax of a lambda is simple –


Key points are –

  • Concise – Before Java8 we would use anonymous classes to deliver “similar” functionality
  • Anonymous – no explicit name
  • Function – not associated with a particular class
  • Argument – can be passed as argument or Stored in a variable

Functional Interfaces

The key requirement for a lambda is that the interface it implements can only have a Single Abstract Method(SAM). However they can have any number of default and static methods

@FunctionalInterface Annotation

@FunctionalInterface is Optional, and enforces this rule by generating a compiler error –

Java8 contains a number of useful interfaces in java.util.function –

Functional Descriptor

Describes the signature of the Functional Interface –

Accessing Class Variables

  • final or “effectively final”(not changed after initialization)

But trying to change effectivelyFinalString results stops it compiling –

The reason this works is that the lambda is accessing a copy of the final or “effectively final” field.

Method References – ::

The final piece of the lambda jigsaw is Method References. Method references are simply a further shorthand in the body of the lambda – so where we have a lambda –

We see map has the lambda – w -> w.getName(). This represents this method –

A method reference lets us shorthand that too –

As with a lot of Java8 the purpose of this shorthand is to allow us to write cleaner more concise code

There are 4 types of method reference in Java 8 –

static method ContainingClass::staticMethodName
instance method of a particular object containingObject::instanceMethodName
instance method of an arbitrary object of a particular type ContainingType::methodName
constructor ClassName::new

About the Author Martin Farrell

My name is Martin Farrell. I have almost 20 years Java experience. I specialize inthe Spring Framework and JEE. I’ve consulted to a range of businesses, and have provide Java and Spring mentoring and training. You can learn more at About

follow me on:

Leave a Comment: