Overview Lambda Streams

3,688 views

Published on

This presentation was created by Stuart Marks and was used at Japanese JUG event (JJUG CCC) at 5/15(Sun).

Published in: Technology, News & Politics
0 Comments
16 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,688
On SlideShare
0
From Embeds
0
Number of Embeds
826
Actions
Shares
0
Downloads
50
Comments
0
Likes
16
Embeds 0
No embeds

No notes for slide

Overview Lambda Streams

  1. 1. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.1
  2. 2. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.2 The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle s products remains at the sole discretion of Oracle.
  3. 3. Overview of Java 8 Lambda and Streams Stuart W. Marks Principal Member of Technical Staff Twitter: @stuartmarks
  4. 4. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.4 What is a Lambda?   A lambda is a function.   A function is a computation that takes parameters and returns a value.   Until Java 8, functions could only be implemented using methods.   A lambda enables functions to be passed around or stored like data.
  5. 5. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.5 Example: Sorting a List of Objects class Person { String name; int age; String getName() { return name; } int getAge() { return age; } } List<Person> list = ... ; Collections.sort(list, ??? );
  6. 6. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.6 Example: Sorting a List of Objects List<Person> list = ... ; public int compare(Person p1, Person p2) { return p1.getName().compareTo(p2.getName()); } Collections.sort(list, ??? );
  7. 7. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.7 Example: Sorting a List of Objects List<Person> list = ... ; class ComparePersonsByName implements Comparator<Person> { @Override public int compare(Person p1, Person p2) { return p1.getName().compareTo(p2.getName()); } } Collections.sort(list, ??? );
  8. 8. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.8 Example: Sorting a List of Objects List<Person> list = ... ; class ComparePersonsByName implements Comparator<Person> { @Override public int compare(Person p1, Person p2) { return p1.getName().compareTo(p2.getName()); } } Collections.sort(list, new ComparePersonsByName());
  9. 9. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.9 Example: Sorting a List of Objects List<Person> list = ... ; Collections.sort(list, new Comparator<Person>() { @Override public int compare(Person p1, Person p2) { return p1.getName().compareTo(p2.getName()); } });
  10. 10. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.10 Example: Sorting a List of Objects List<Person> list = ... ; Collections.sort(list, (Person p1, Person p2) { return p1.getName().compareTo(p2.getName()); } );
  11. 11. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.11 Example: Sorting a List of Objects List<Person> list = ... ; Collections.sort(list, (Person p1, Person p2) -> p1.getName().compareTo(p2.getName()) );
  12. 12. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.12 Example: Sorting a List of Objects List<Person> list = ... ; Collections.sort(list, (p1, p2) -> p1.getName().compareTo(p2.getName()) );
  13. 13. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.13 Functional Interfaces interface Runnable { public void run(); } new Thread(new Runnable() { @Override public void run() { System.out.println("Hello, thread!"); } }); new Thread(() -> System.out.println("Hello, thread!"));
  14. 14. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.14 New APIs Using Functional Interfaces List<Person> list = ... ; for (Person p : list) { System.out.println(p); } list.forEach(p -> System.out.println(p));
  15. 15. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.15 New APIs Using Functional Interfaces List<Person> list = Collections.synchronizedList(...); for (Person p : list) { System.out.println(p); } list.forEach(p -> System.out.println(p));
  16. 16. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.16 New APIs Using Functional Interfaces List<Person> list = ... ; for (Person p : list) { if ("Jones".equals(p.getName())) { ??? how to remove this Person ??? } }
  17. 17. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.17 New APIs Using Functional Interfaces List<Person> list = ... ; for (Iterator<Person> iter = list.iterator(); iter.hasNext(); ) { Person p = iter.next(); if ("Jones".equals(p.getName())) { iter.remove(); } } list.removeIf(p -> "Jones".equals(p.getName()));
  18. 18. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.18 New APIs Using Functional Interfaces List<String> list = Arrays.asList("Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Golf"); for (ListIterator<String> iter = list.listIterator(); iter.hasNext(); ) { iter.set(iter.next().toUpperCase()); } list.replaceAll(s -> s.toUpperCase()); list.replaceall(String::toUpperCase); // method reference
  19. 19. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.19 Default Methods Added to Collections Iterable.forEach(lambda) Collection.removeIf(lambda) List.replaceAll(lambda) List.sort(lambda) // replaces static method Collections.sort()
  20. 20. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.20 Default Methods Added to Collections Collections.sort(list, (p1, p2) -> p1.getName().compareTo(p2.getName()) ); list.sort( (p1, p2) -> p1.getName().compareTo(p2.getName()));
  21. 21. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.21 Combining Operations   New collection operations include forEach, removeIf, replaceAll   Each operates on a collection, possibly modifying it   What if we want to combine these operations? –  for example, operate only certain elements that meet some criterion   Using collections, we'd need to copy and create temporary collections   Solution: streams
  22. 22. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.22 Streams   Streams: a multiplicity of values –  no storage –  might or might not be ordered –  sort-of like an iterator –  affords serial or parallel processing   Stream pipeline: –  a source –  zero or more intermediate operations –  a terminal operation
  23. 23. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.23 Streams   Sources –  collections, array, varargs parameters, numeric range, lines of file   Intermediate operations –  filter, map, distinct, sorted, skip, limit, flatMap   Terminal operations –  forEach, toArray, collect, reduce, count, min, max –  findFirst, findAny, anyMatch, allMatch, noneMatch
  24. 24. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.24 Streams Example // Given a list of persons, generate a sorted list of // names that longer than longer than 3 characters. List<Person> persons = ... ; persons.stream() .map(p -> p.getName()) .filter(s -> s.codePointCount(0, s.length()) > 3) .sorted() .forEach(s -> System.out.println(s));
  25. 25. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.25 Streams Example // Given a list of persons, generate a sorted list of // names that longer than longer than 3 characters. List<Person> persons = ... ; persons.stream() .map(p -> p.getName()) .filter(s -> s.codePointCount(0, s.length()) > 3) .sorted() .forEach(System.out::println);
  26. 26. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.26 Streams Example // Given a list of persons, generate a sorted list of // names that longer than longer than 3 characters. List<Person> persons = ... ; List<String> sortedNames = persons.stream() .map(p -> p.getName()) .filter(s -> s.codePointCount(0, s.length()) > 3) .sorted() .collect(Collectors.toList());
  27. 27. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.27 Streams Example // Calculate pi using the Taylor Series // pi = 4 * (1 – 1/3 + 1/5 – 1/7 + ...) System.out.println( LongStream.range(0L, 1_000_000_000L) .map(i -> ((i & 1) == 0 ? 1 : -1) * (2 * i + 1)) .mapToDouble(i -> 4.0 / i) .sum());
  28. 28. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.28 Streams Example // Calculate pi using the Taylor Series // pi = 4 * (1 – 1/3 + 1/5 – 1/7 + ...) System.out.println( LongStream.range(0L, 1_000_000_000L) .parallel() .map(i -> ((i & 1) == 0 ? 1 : -1) * (2 * i + 1)) .mapToDouble(i -> 4.0 / i) .sum());
  29. 29. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.29 Summary   Lambdas: anonymous functions   Default methods   Method references   Streams: allow pipelined operations on multiple values –  programmed using lambda –  enable parallelism Lambdas and streams make code read more like the problem statement, resulting in code that is clear, concise, and easy to maintain.
  30. 30. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.30 Java 8 Resources   Download: java.oracle.com   Documentation & Tutorials: docs.oracle.com/javase   Source code: openjdk.java.net/projects/jdk8   FAQ: lambdafaq.org   Technical documents: openjdk.java.net/projects/lambda –  State of the Lambda –  State of the Lambda: Libraries Edition
  31. 31. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.31 The preceding is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle s products remains at the sole discretion of Oracle.
  32. 32. Copyright © 2014, Oracle and/or its affiliates. All rights reserved.32

×