Your SlideShare is downloading. ×
Lambda Expressions in Java 8
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Lambda Expressions in Java 8

1,406

Published on

Lambda Expressions in Java 8

Lambda Expressions in Java 8

Published in: Technology, Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,406
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
27
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 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<Person>(){ public int compare(Person p1, Person p2){ return p1.firstName.compareTo(p2.firstName); } });
  • 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. 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<Employee> old = new ArrayList<Employee>(); for (Employee e : employeeList) { if (e.age() > 60) { old.add(e); } }
  • 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. 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. 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. 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. 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. map List<String> names = new ArrayList<String>(); for (Employee e : employeeList) { names.add(e.firstName() + " " + e.lastName()); }
  • 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. 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 query reduce() => aggregate functions sort() => ORDER BY
  • 22. λ Java 8
  • 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. Thank you for listening! bryanbibat.net | @bry_bibat speakerdeck.com/bryanbibat

×