Created
October 4, 2015 04:55
-
-
Save sohlich/6ed950c513c71aabdf6a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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