Содержание
- 2. Outline Stream Building Blocks Java 8 Default Methods Functional Interfaces Lambda Expressions Method References
- 3. Outline Characteristics of Streams Creating Streams Common Functional Interfaces Used Anatomy of the Stream pipeline Optional
- 4. Java 8 Target Release Date: 03/18/14 Introduces Default Methods Functional Interfaces Lambda Expressions Stream API and
- 5. Default Methods In Context of Support For Streams Java 8 needed to add functionality to existing
- 6. Default Methods Problem Pre-Java 8 interfaces couldn’t have method bodies. The only way to add functionality
- 7. Default Methods Solution Default Methods! Java 8 allows default methods to be added to interfaces with
- 8. Default Methods Example public interface A { default void foo(){ System.out.println("Calling A.foo()"); } public class Clazz
- 9. Functional Interfaces Interfaces with only one abstract method. With only one abstract method, these interfaces can
- 10. Lambda expressions A more brief and clearly expressive way to implement functional interfaces Format: -> Example
- 11. Method References Event more brief and clearly expressive way to implement functional interfaces Format: :: Example
- 12. Characteristics of Streams Streams are not related to InputStreams, OutputStreams, etc. Streams are NOT data structures
- 13. Creating Streams From individual values Stream.of(val1, val2, …) From array Stream.of(someArray) Arrays.stream(someArray) From List (and other
- 14. Common Functional Interfaces Used Predicate Represents a predicate (boolean-valued function) of one argument Functional method is
- 15. Common Functional Interfaces Used Function Represents a function that accepts one argument and produces a result
- 16. Common Functional Interfaces Used UnaryOperator Represents an operation on a single operands that produces a result
- 17. Common Functional Interfaces Used BiFunction Represents an operation that accepts two arguments and produces a result
- 18. Anatomy of the Stream Pipeline A Stream is processed through a pipeline of operations A Stream
- 19. Anatomy of the Stream Pipeline Intermediate Methods map, filter, distinct, sorted, peek, limit, parallel Terminal Methods
- 20. Optional Class A container which may or may not contain a non-null value Common methods isPresent()
- 21. Common Stream API Methods Used Void forEach(Consumer) Easy way to loop over Stream elements You supply
- 22. Common Stream API Methods Used Void forEach(Consumer) Example Employees.forEach(e -> e.setSalary(e.getSalary() * 11/10)) Give all employees
- 23. Common Stream API Methods Used Void forEach(Consumer) Vs. For Loops List employees = getEmployees(); for(Employee e:
- 24. Common Stream API Methods Used Stream map(Function) Produces a new Stream that is the result of
- 25. Common Stream API Methods Used Stream filter(Predicate) Produces a new Stream that contains only the elements
- 26. Common Stream API Methods Used Optional findFirst() Returns an Optional for the first entry in the
- 27. Common Stream API Methods Used Object[] toArray(Supplier) Reads the Stream of elements into a an array
- 28. Common Stream API Methods Used List collect(Collectors.toList()) Reads the Stream of elements into a List or
- 29. Common Stream API Methods Used List collect(Collectors.toList()) partitioningBy You provide a Predicate. It builds a Map
- 30. Common Stream API Methods Used T reduce(T identity, BinaryOperator) You start with a seed (identity) value,
- 31. Common Stream API Methods Used Stream limit(long maxSize) Limit(n) returns a stream of the first n
- 32. Common Stream API Methods Used Stream skip(long n) skip(n) returns a stream starting with element n
- 33. Common Stream API Methods Used Stream sorted(Comparator) Returns a stream consisting of the elements of this
- 34. Common Stream API Methods Used Optional min(Comparator) Returns the minimum element in this Stream according to
- 35. Common Stream API Methods Used Optional max(Comparator) Returns the minimum element in this Stream according to
- 36. Common Stream API Methods Used Stream distinct() Returns a stream consisting of the distinct elements of
- 37. Common Stream API Methods Used Boolean anyMatch(Predicate), allMatch(Predicate), noneMatch(Predicate) Returns true if Stream passes, false otherwise
- 38. Common Stream API Methods Used long count() Returns the count of elements in the Stream Example
- 39. Parallel Streams Helper Methods For Timing private static void timingTest(Stream testStream) { long startTime = System.nanoTime();
- 40. Parallel Streams Helper Method For Simulating Long Operation void doSlowOp() { try { TimeUnit.SECONDS.sleep(1); } catch
- 41. Parallel Streams Main Code System.out.print("Serial version [11 entries]:"); timingTest(googlers()); int numProcessorsOrCores = Runtime.getRuntime().availableProcessors(); System.out.printf("Parallel version on
- 42. Parallel Streams Results Serial version [11 entries]: 11.000 seconds. Parallel version on 4-core machine: 3.000 seconds.
- 43. (On The Fly) Streams Stream generate(Supplier) The method lets you specify a Supplier This Supplier is
- 44. References Stream API http://download.java.net/jdk8/docs/api/java/util/stream/Stream.html Java 8 Explained: Applying Lambdas to Java Collections http://zeroturnaround.com/rebellabs/java-8-explained-applying-lambdas-to-java-collections/ Java 8 first
- 46. Скачать презентацию