Session Details

Learning to Love the Lambda in the Stream

Regular Session

Introduction to Java Lambda expressions

  1. What is a Lambda?
  2. Functional Interfaces (FI) in Java 8
  3. Assigning a Lambda to a FI.
  4. Compare a Lambda to an anonymous inner class

The Built In Java FIs

  1. Predicate
  2. Supplier
  3. Consumer
  4. Function
  5. Comparable, Comparator - Segway back to definition of FIs, that pre Java 8 interfaces are FIs if they have exactly 1 abstract method.

Our First Stream: Add Integer Elements in a Collection

  1. Show adding example using a traditional imperative programming loop
  2. Show adding example using a Stream.
  3. Show adding example using parallel stream. Since addition is commutative and associative, it is ideal for parallel processing
  4. Show adding example using fork / join. Not nearly as nice as using the stream
  5. This operation is a reduction, because it creates one value based processing the stream elements

New requirement: Skip numbers divisible by 4

  1. Review the predicate interface
  2. Define the stream using Stream.of instead of collection
  3. Create a predicate lambda for numbers not divisible by 4
  4. Use the filter operation on the stream with the predicate.
  5. Use Stream.forEach(System.out::println) to check list.
  6. Example of a method reference bound to a Consumer interface.
  7. Review Consumer interface.
  8. Compute the sum without the numbers divisible by 4
  9. Get the sum, average, min, max, and count using statistics.

Get sum of numbers divisible by 4, and of numbers not divisible by 4

  1. Introduce partitions
  2. Define stream using IntStream.rangeClosed
  3. Use predicate lambda to create the partitions using partitioningBy collector.
  4. Use a collector on the partition to sum the elements (reduce each partition)

Get sum of N modulo 4

  1. Introduce Grouping
  2. Partitioning is special case of grouping
  3. Use function lambda to take modulus of 4 for map key
  4. Use groupingBy to group the map with N % 4 as the keys.
  5. Show map with values for keys 0..3.
  6. Use a collection on the grouping to sum the elements (reduce each grouping)

Infinite Streams

  1. Reductions, such as a sum will run forever.
  2. Introduce findFirst, findAny, allMatch, anyMatch, noneMatch predicate operations.
  3. findFirst and findAny will always terminate.
  4. Termination of allMatch, anyMatch, and noneMatch depend on the underlying data.
  5. Use the limit operation to convert an infinite stream into a finite stream.
  6. Use skip to skip element processing.
    • When used on an infinite stream, the result is an infinite stream with the specified number of elements skipped.
    • When used on a finite stream, the elements have those elements skipped, with correspondingly fewer elements remaining for processing.
    • A skip operation that precedes a limit operation will have no effect on how many elements are processed by the stream resulting from the limit operation
    • A skip operation that succeeds a limit operation will reduce the number of elements to be processed by the number specified in the skip operation
  7. Use the supplier FI to produce and process an infinite stream of random numbers

Unit Testing

  1. Review Builder design pattern
  2. Show how the builder design pattern can be used to insert support unit tests.
    • Unit Test supplies a builder that appends the peek intermediate operation to the stream. The peek operation may be used observe and test the data from the stream.
    • Production code uses a default identity builder, that is a builder that returns the stream it is given without adding any operations to it.

Summary

Java 8 lambdas and streams introduce a powerful new framework into the java language. This framework allows complex and possibly parallel computations to be expressed succinctly and performed efficiently. Streams are a complement to Object Oriented Programming: OO Programming has traditionally allowed us to override operations in subclasses but keep control structures in suberclasses. Streams allow the control structures themselves to be abstracted away, allowing the developer to focus on the problem at hand and leaving the details to the stream framework. The builder pattern may be used to support unit testing of streams.  Lambdas and streams provide a way to learn and experiment with functional programming concepts without the daunting task of also learning a new programming language. Frequent use of lambdas will make learning a functional programming language and ecosystem an easier task, because the functional programming concepts will have familiarity.

The contents of the Notes for Reviewers and Prerequisites disappear every time I save them so I am writing them here.

Notes for Reviewers
The presentation and example code will be placed in GitHub prior to the presentation. The example code will be well documented to illustrate each point during the presentation. It will be run at the corresponding point in the presentation to show that the lambdas and streams work as advertised.

Prerequisites
Basic knowledge of Java Programming: loops and Collection. Functional programming is not a prerequisite.


Richard Roda

Sr Software Engineer
Hewlett Packard Enterprise

About

A software development conference in the Louisville, KY area on June 7-9, 2017 designed to cover all aspects of software development regardless of development stack.

Contact Us
Keep in Touch