O'Reilly logo
live online training icon Live Online training

Functional Programming in Java

Working with streams, lambda expressions, and method references in Java SE8 and beyond

Ken Kousen

Today’s software developers are embracing functional programming (FP) in droves, whether it’s to write concurrent programs or to manage Big Data. But where does that leave Java developers? In this two-day, hands-on, online course, you’ll be introduced to such constructs as lambda expressions, method references, and the Java Streams API. This is a code-driven course for the Java programmer who wants real hands-on experience working with FP.

By the end of this course, you’ll have a thorough understanding of why the new functional capabilities were introduced into Java SE8, how FP works within data pipelines, and how this FP style promotes terse and simple-to-modify code, run-time efficiency, and thread safety. You'll also be designing and writing more efficient applications using the new standards in Java from version SE8 and beyond.

What you'll learn-and how you can apply it

By the end of this live, hands-on course, you’ll understand:

  • How to use the new functional features added to Java 8, but available in versions 9, 10, 11, and beyond
  • How to replace iteration with transformations
  • How to implement the new syntax for lambda expressions and method references into your Java applications
  • How to use lambda expressions in maps and filters
  • How and when to use the new Optional data type
  • How to work with the new Stream model
  • How functional concepts support concurrency

And you’ll be able to:

  • Replace loops and conditionals with transformations and filters
  • Use the new functional interfaces in the Java API
  • Work with functional concepts, like map/filter/reduce
  • Use Collectors and Comparators in stream programming

This training course is for you because...

  • You are a Java developer working with Java SE7 or earlier
  • You are a member of a software team that has adopted a tool that supports the new features in Java SE8, 9, 10 or later
  • You wish to simplify Java code using the new functional techniques in Java

Prerequisites

  • Working knowledge of Java prior to version 8

Note: Courseware will be accessible online, and all code samples will be made available in a public GitHub repository.

Setup information will be provided before the course, but the preferred setup will use Java 8 Standard Edition, the Gradle Build Tool (a wrapper will be included in the courseware), and an IDE that understands Java (preferably IntelliJ IDEA, though other options are supported). Support for new features added in SE9, 10, and beyond will be discussed during the class.

Recommended Preparation:

About your instructor

Schedule

The timeframes are only estimates and may vary according to how the class is progressing

DAY 1

Functional Interfaces (15 min)

  • Single Abstract Method (SAM) interfaces
  • Implementing SAM interfaces

Lambda Expressions and Method References (30 min)

  • The new lambda syntax
  • Block lambdas and expression lambdas
  • Method references and Constructor references

Exercise: Implementing functional interfaces with lambdas (15 min)

Static and Default Methods in Interfaces (20 min)

  • Default methods
  • Static methods
  • Resolving conflicts
  • Static and default methods in the Java API

Exercise: Static and default interface methods (10 min)

Break: 15 min

The java.util.function Package (30 min)

  • Consumers
  • Suppliers
  • Functions
  • Predicates
  • UnaryOperators and BinaryOperators
  • Primitive specializations

Streams (30 min)

  • The java.util.Stream interface
  • Primitive streams and object streams
  • Map and filter operations
  • Built-in reduction operations

Exercise: Stream processing (20 min)

Collect and Comparator (30 min)

  • Collecting to lists and sets
  • Collecting to other data structures
  • Implementing comparators using lambdas
  • The comparing methods

Exercise: Sorting POJOs by multiple conditions (15 min)

DAY 2

Review of Day 1 material and Q/A (15 min)

The Optional Class (30 min)

  • Creating optionals
  • Using Optional in DAO layers
  • Methods on Optional

Exercise: Adding Optional to existing types (30 min)

File I/O (15 min)

  • Streams in NIO
  • Autocloseable streams with try-with-resources

Exercise: Processing files line by line (15 min)

Break (15 min)

Miscellaneous New Features (30 min)

  • Deferred execution
  • Using peek to examine stream data
  • Boxed streams
  • Closure composition

Exercise: Debugging streams (15 min)

The java.time Package (15 min)

  • Immutable types and factory methods
  • Time zone issues
  • Periods and durations
  • Interesting examples

Summary and References (10 min)