Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Optional Parameters in Java 8 Lambda Expressions

DZone's Guide to

Optional Parameters in Java 8 Lambda Expressions

· Java Zone
Free Resource

The single app analytics solutions to take your web and mobile apps to the next level.  Try today!  Brought to you in partnership with CA Technologies

Yeah, they don't really exist, but we can use polymorphism, method overloading and default methods instead to make it a bit more convenient to use our APIs.

As an example, here's an event bus implementation where I can register event handlers with an optional header parameter.

  Bus bus = new Bus();
  bus.register(event -> System.out.println("I gots an event"));
  bus.register((event,header) -> System.out.println("I gots an event w/ header"));

Here are the dirty details on how you can do this (and - when dispatching - events, use default methods to avoid type coercion.

CA App Experience Analytics, a whole new level of visibility. Learn more. Brought to you in partnership with CA Technologies.

Topics:
java8 ,lambda

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}