Advertisement
Guest User

Untitled

a guest
Oct 3rd, 2015
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. // with no parameter
  2. () -> System.out.println("Hello, world.")
  3.  
  4. // with a single parameter (This example is an identity function).
  5. a -> a
  6.  
  7. // with a single expression
  8. (a, b) -> a + b
  9.  
  10. // with explicit type information
  11. (long id, String name) -> "id: " + id + ", name:" + name
  12.  
  13. // with a code block
  14. (a, b) -> { return a + b; }
  15.  
  16. // with multiple statements in the lambda body. It requires a code block.
  17. // This example also includes two nested lambda expressions (the first one is also a closure).
  18. (id, defaultPrice) -> {
  19. Optional<Product> product = productList.stream().filter(p -> p.getId() == id).findFirst();
  20. return product.map(p -> p.getPrice()).orElse(defaultPrice);
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement