Skip to content

Instantly share code, notes, and snippets.

@sohlich
Created October 4, 2015 04:55
Show Gist options
  • Save sohlich/6ed950c513c71aabdf6a to your computer and use it in GitHub Desktop.
Save sohlich/6ed950c513c71aabdf6a to your computer and use it in GitHub Desktop.
// with no parameter
() -> System.out.println("Hello, world.")
// with a single parameter (This example is an identity function).
a -> a
// with a single expression
(a, b) -> a + b
// with explicit type information
(long id, String name) -> "id: " + id + ", name:" + name
// with a code block
(a, b) -> { return a + b; }
// with multiple statements in the lambda body. It requires a code block.
// This example also includes two nested lambda expressions (the first one is also a closure).
(id, defaultPrice) -> {
Optional<Product> product = productList.stream().filter(p -> p.getId() == id).findFirst();
return product.map(p -> p.getPrice()).orElse(defaultPrice);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment