Lambda Expressions in Java 8

download Lambda Expressions in Java 8

of 25

  • date post

    10-May-2015
  • Category

    Technology

  • view

    1.427
  • download

    5

Embed Size (px)

description

Lambda Expressions in Java 8

Transcript of Lambda Expressions in Java 8

  • 1.

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. Ang Java ay parang kami ng ex ko.....wala paring closure. - anon 4. Anonymous Inner Classes Collections.sort(personList, new Comparator(){ public int compare(Person p1, Person p2){ return p1.firstName.compareTo(p2.firstName); } }); 5. Anonymous Inner Classes Collections.sort(personList, new Comparator(){ 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. 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. JEP 107 Bulk Data Operations for Collections (e.g filter/map/reduce) 8. filter List old = new ArrayList(); for (Employee e : employeeList) { if (e.age() > 60) { old.add(e); } } 9. filter List old = new ArrayList(); for (Employee e : employeeList) { if (e.age() > 60) { old.add(e); } }List old = employeeList.stream() .filter(e -> e.age() > 60) .collect( Collectors.toCollection( () -> new ArrayList() ) ); 10. filter List old = new ArrayList(); for (Employee e : employeeList) { if (e.age() > 60) { old.add(e); } }List old = employeeList.stream() .filter(e -> e.age() > 60) .collect(Collectors.toCollection( () -> new ArrayList())); 11. filter List old = new ArrayList(); for (Employee e : employeeList) { if (e.age() > 60) { old.add(e); } }List old = employeeList.stream() .filter(e -> e.age() > 60) .collect( Collectors.toCollection( ArrayList::new ) ); 12. filter List old = new ArrayList(); for (Employee e : employeeList) { if (e.age() > 60) { old.add(e); } }List old = employeeList.stream() .filter(e -> e.age() > 60) .collect(Collectors.toCollection(ArrayList::new)); 13. filter List old = new ArrayList(); for (Employee e : employeeList) { if (e.age() > 60) { old.add(e); } }List old = employeeList.stream() .filter(e -> e.age() > 60) .collect(Collectors.toList()); 14. map List names = new ArrayList(); for (Employee e : employeeList) { names.add(e.firstName() + " " + e.lastName()); } 15. map List names = new ArrayList(); for (Employee e : employeeList) { names.add(e.firstName() + " " + e.lastName()); }List names = employeeList.stream() .map(e -> e.firstName() + " " + e.lastName()) .collect(Collectors.toList()); 16. reduce BigDecimal totalSalary = BigDecimal.ZERO; for (Employee e : employeeList) { totalTax = totalTax.add(e.salary()); } 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. 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. 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. 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. just like SQL filter() => WHERE, GROUP BY map()=> SELECT, inline queryreduce() => aggregate functions sort()=> ORDER BY 22. Java 8 23. Where to get Lambda Project Pagehttp://openjdk.java.net/projects/lambda/JDK8 http://jdk8.java.net/download.htmlJDK8 w/ Lambda http://jdk8.java.net/lambda/NetBeans Nightlyhttp://bertram2.netbeans.org:8080/job/jdk8lambda/lastSuccessfulBuild/artifact/nbbuild/ 24. Thank you for listening!bryanbibat.net | @bry_bibat speakerdeck.com/bryanbibat