lambda expressions in java 8

25
λ

Upload: bryanbibat

Post on 10-May-2015

1.442 views

Category:

Technology


5 download

DESCRIPTION

Lambda Expressions in Java 8

TRANSCRIPT

Page 1: Lambda Expressions in Java 8

λ

Page 2: Lambda Expressions in Java 8
Page 3: Lambda Expressions in Java 8

functions as values

var square = function(x) { return x * x };

var apply = function(data, func) { return func(data);}

console.log(apply(10, square));

Page 4: Lambda Expressions in Java 8

Ang Java ay parangkami ng ex ko...

..wala paring closure.

- anon

Page 5: Lambda Expressions in Java 8

Anonymous Inner ClassesCollections.sort(personList, new Comparator<Person>(){ public int compare(Person p1, Person p2){ return p1.firstName.compareTo(p2.firstName); }});

Page 6: Lambda Expressions in Java 8

Anonymous Inner Classes

Lambda Expressions

Collections.sort(personList, new Comparator<Person>(){ public int compare(Person p1, Person p2){ return p1.firstName.compareTo(p2.firstName); }});

Collections.sort(personList, (Person p1, Person p2) -> p1.firstName.compareTo(p2.firstName));

Page 7: Lambda Expressions in Java 8

Lambda Expressions

Lambda Exp. (shorter)

Collections.sort(personList, (Person p1, Person p2) -> p1.firstName.compareTo(p2.firstName));

Collections.sort(personList, (p1, p2) -> p1.firstName.compareTo(p2.firstName));

Page 8: Lambda Expressions in Java 8

JEP 107Bulk Data Operations

for Collections(e.g filter/map/reduce)

Page 9: Lambda Expressions in Java 8

filterList<Employee> old = new ArrayList<Employee>();

for (Employee e : employeeList) { if (e.age() > 60) { old.add(e); }}

Page 10: Lambda Expressions in Java 8

filterList<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<>() ) );

Page 11: Lambda Expressions in Java 8

filterList<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<>()));

Page 12: Lambda Expressions in Java 8

filterList<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 ) );

Page 13: Lambda Expressions in Java 8

filterList<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));

Page 14: Lambda Expressions in Java 8

filterList<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());

Page 15: Lambda Expressions in Java 8

mapList<String> names = new ArrayList<String>();

for (Employee e : employeeList) { names.add(e.firstName() + " " + e.lastName());}

Page 16: Lambda Expressions in Java 8

mapList<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());

Page 17: Lambda Expressions in Java 8

reduceBigDecimal totalSalary = BigDecimal.ZERO;

for (Employee e : employeeList) { totalTax = totalTax.add(e.salary());}

Page 18: Lambda Expressions in Java 8

reduceBigDecimal 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));

Page 19: Lambda Expressions in Java 8

reduceBigDecimal 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);

Page 20: Lambda Expressions in Java 8

chainingBigDecimal totalOldTax = employeeList.stream() .filter(e -> e.age() > 60) .map(e -> e.salary().multiply(e.taxRate())) .reduce(BigDecimal.ZERO, (sum, tax) -> sum.add(tax);

Page 21: Lambda Expressions in Java 8

just like SQLBigDecimal 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 employeesWHERE age > 60;

Page 22: Lambda Expressions in Java 8

just like SQL

filter() => WHERE, GROUP BY

map() => SELECT, inline query

reduce() => aggregate functions

sort() => ORDER BY

Page 23: Lambda Expressions in Java 8

λJava 8

Page 24: Lambda Expressions in Java 8

Where to get LambdaProject Pagehttp://openjdk.java.net/projects/lambda/

JDK8http://jdk8.java.net/download.html

JDK8 w/ Lambdahttp://jdk8.java.net/lambda/

NetBeans Nightlyhttp://bertram2.netbeans.org:8080/job/jdk8lambda/lastSuccessfulBuild/artifact/nbbuild/

Page 25: Lambda Expressions in Java 8

Thank you for listening!

bryanbibat.net | @bry_bibatspeakerdeck.com/bryanbibat