Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Lambda Expressions in Java 8

2,308 views

Published on

Lambda Expressions in Java 8

Published in: Technology, Education
  • Be the first to comment

Lambda Expressions in Java 8

  1. 1. λ
  2. 2. functions as values var square = function(x) { return x * x }; var apply = function(data, func) { return func(data); } console.log(apply(10, square));
  3. 3. Ang Java ay parang kami ng ex ko... ..wala paring closure. - anon
  4. 4. Anonymous Inner Classes Collections.sort(personList, new Comparator<Person>(){ public int compare(Person p1, Person p2){ return p1.firstName.compareTo(p2.firstName); } });
  5. 5. Anonymous Inner Classes Collections.sort(personList, new Comparator<Person>(){ public int compare(Person p1, Person p2){ return p1.firstName.compareTo(p2.firstName); } }); Lambda Expressions Collections.sort(personList, (Person p1, Person p2) -> p1.firstName.compareTo(p2.firstName));
  6. 6. Lambda Expressions Collections.sort(personList, (Person p1, Person p2) -> p1.firstName.compareTo(p2.firstName)); Lambda Exp. (shorter) Collections.sort(personList, (p1, p2) -> p1.firstName.compareTo(p2.firstName));
  7. 7. JEP 107 Bulk Data Operations for Collections (e.g filter/map/reduce)
  8. 8. filter List<Employee> old = new ArrayList<Employee>(); for (Employee e : employeeList) { if (e.age() > 60) { old.add(e); } }
  9. 9. filter List<Employee> old = new ArrayList<Employee>(); for (Employee e : employeeList) { if (e.age() > 60) { old.add(e); } } List<Employee> old = employeeList.stream() .filter(e -> e.age() > 60) .collect( Collectors.toCollection( () -> new ArrayList<>() ) );
  10. 10. filter List<Employee> old = new ArrayList<Employee>(); for (Employee e : employeeList) { if (e.age() > 60) { old.add(e); } } List<Employee> old = employeeList.stream() .filter(e -> e.age() > 60) .collect(Collectors.toCollection( () -> new ArrayList<>()));
  11. 11. filter List<Employee> old = new ArrayList<Employee>(); for (Employee e : employeeList) { if (e.age() > 60) { old.add(e); } } List<Employee> old = employeeList.stream() .filter(e -> e.age() > 60) .collect( Collectors.toCollection( ArrayList::new ) );
  12. 12. filter List<Employee> old = new ArrayList<Employee>(); for (Employee e : employeeList) { if (e.age() > 60) { old.add(e); } } List<Employee> old = employeeList.stream() .filter(e -> e.age() > 60) .collect(Collectors.toCollection(ArrayList::new));
  13. 13. filter List<Employee> old = new ArrayList<Employee>(); for (Employee e : employeeList) { if (e.age() > 60) { old.add(e); } } List<Employee> old = employeeList.stream() .filter(e -> e.age() > 60) .collect(Collectors.toList());
  14. 14. map List<String> names = new ArrayList<String>(); for (Employee e : employeeList) { names.add(e.firstName() + " " + e.lastName()); }
  15. 15. map List<String> names = new ArrayList<String>(); for (Employee e : employeeList) { names.add(e.firstName() + " " + e.lastName()); } List<String> names = employeeList.stream() .map(e -> e.firstName() + " " + e.lastName()) .collect(Collectors.toList());
  16. 16. reduce BigDecimal totalSalary = BigDecimal.ZERO; for (Employee e : employeeList) { totalTax = totalTax.add(e.salary()); }
  17. 17. reduce BigDecimal totalSalary = BigDecimal.ZERO; for (Employee e : employeeList) { totalTax = totalTax.add(e.salary()); } BigDecimal totalSalary = employeeList.stream() .reduce(BigDecimal.ZERO, (sum, e) -> sum.add(e.salary()), (bd1, bd2) -> bd1.add(bd2));
  18. 18. reduce BigDecimal totalSalary = BigDecimal.ZERO; for (Employee e : employeeList) { totalTax = totalTax.add(e.salary()); } BigDecimal totalSalary = employeeList.stream() .reduce(BigDecimal.ZERO, (sum, e) -> sum.add(e.salary()), BigDecimal::add);
  19. 19. chaining BigDecimal totalOldTax = employeeList.stream() .filter(e -> e.age() > 60) .map(e -> e.salary().multiply(e.taxRate())) .reduce(BigDecimal.ZERO, (sum, tax) -> sum.add(tax);
  20. 20. just like SQL BigDecimal totalOldTax = employeeList.stream() .filter(e -> e.age() > 60) .map(e -> e.salary().multiply(e.taxRate())) .reduce(BigDecimal.ZERO, (sum, tax) -> sum.add(tax); SELECT SUM(salary * tax_rate) FROM employees WHERE age > 60;
  21. 21. just like SQL filter() => WHERE, GROUP BY map() => SELECT, inline query reduce() => aggregate functions sort() => ORDER BY
  22. 22. λ Java 8
  23. 23. Where to get Lambda Project Page http://openjdk.java.net/projects/lambda/ JDK8 http://jdk8.java.net/download.html JDK8 w/ Lambda http://jdk8.java.net/lambda/ NetBeans Nightly http://bertram2.netbeans.org:8080/job/jdk8lambda/lastSuccessfulBuild/artifact/nbbuild/
  24. 24. Thank you for listening! bryanbibat.net | @bry_bibat speakerdeck.com/bryanbibat

×