Java 8 Lambda Expressions

7,708 views

Published on

Slides for a lightning talk on Java 8 lambda expressions I gave at the Near Infinity (www.nearinfinity.com) 2013 spring conference.

The associated sample code is on GitHub at https://github.com/sleberknight/java8-lambda-samples

Published in: Technology, News & Politics
2 Comments
13 Likes
Statistics
Notes
No Downloads
Views
Total views
7,708
On SlideShare
0
From Embeds
0
Number of Embeds
13
Actions
Shares
0
Downloads
366
Comments
2
Likes
13
Embeds 0
No embeds

No notes for slide

Java 8 Lambda Expressions

  1. 1. ScottLeberknightJava 8λ Expressions
  2. 2. Mom, why?Promote immutabilityDeclare what to do, not how to do itEasier parallelization & lazy evaluationCleaner, more concise code
  3. 3. List<Integer> numbers =Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);numbers.forEach(new Consumer<Integer>() {@Overridepublic void accept(Integer integer) {System.out.println(integer);}});iterating w/ forEach(under the hood)
  4. 4. lambda expressions(argument list) -> code(final String name, final int age) -> code(name, age) -> codename -> codeexamples:type inference(String name, int age) -> code}λ
  5. 5. iterating w/lambdasList<Integer> numbers =Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);numbers.forEach((final Integer value) -> System.out.println(value));lambda expression
  6. 6. iterating w/lambdas, type inferenceList<Integer> numbers =Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);numbers.forEach(value -> System.out.println(value));
  7. 7. method referencesList<Integer> numbers =Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);numbers.forEach(System.out::println);method reference
  8. 8. transforms using mapList<Integer> numbers =Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);numbers.stream().map(value -> value * 2).forEach(System.out::println);
  9. 9. filteringList<Integer> numbers =Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);numbers.stream().filter(value -> value % 2 == 0).forEach(System.out::println);
  10. 10. filtering v2List<String> names = Arrays.asList("Bob", "Tom", "Jeff", "Scott", "Jennifer", "Steve");final Function<String, Predicate<String>> startsWithLetter =letter -> name -> name.startsWith(letter);names.stream().filter(startsWithLetter.apply("J")).forEach(System.out::println);
  11. 11. Streams.iterate(1, number -> number + 1).map(number -> number * number).limit(25).forEach(number -> System.out.print(number + " "));// 1 4 9 16 25 36 49 64 ... 529 576 625Streams
  12. 12. finding with OptionalList<String> names = Arrays.asList("Bob", "Tom", "Jeff", "Scott", "Jennifer", "Steve");Optional<String> firstS = names.stream().filter(name -> name.startsWith("S")).findFirst();System.out.println(firstS.orElse("None found"));
  13. 13. reducersList<String> names = Arrays.asList("Bob", "Tom", "Jeff", "Scott", "Jennifer", "Steve");Optional<String> longestName = names.stream().reduce((name1, name2) ->name1.length() >= name2.length() ? name1 : name2);if (longestName.isPresent()) {System.out.println(longestName.get());}
  14. 14. sum reducerList<Integer> numbers =Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);long sum = numbers.stream().map(value -> value * value).sum();
  15. 15. parallelizationnumbers.parallelStream().filter(number -> isPerfect(number)).forEach(System.out::println);
  16. 16. how far we went in this short talk...
  17. 17. http://pragprog.com/book/vsjava8/functional-programming-in-java
  18. 18. http://jdk8.java.net/JDK 8 Project
  19. 19. sample code available at:https://github.com/sleberknight/java8-lambda-samples
  20. 20. My Infoscott dot leberknight at nearinfinity dot comtwitter.com/sleberknight www.sleberknight.com/blogwww.nearinfinity.com/blogs/scott_leberknight/all/scott dot leberknight at gmail dot com

×