Refactoring to Streams and Lambdas Live Course - 29-30 November 2021

Java celebrated its 25th birthday in 2020. Code written in 1995 still runs today, without even having to recompile it. This is one of the biggest reasons why Java has had such success in enterprises.

Over the years, we have seen lots of improvements to make Java code easier to work with. Inner classes came in Java 1.1. A proper collection framework joined us in Java 1.2. Java 5 gave us better type safety with generics.

But the biggest improvement for Java programmers came in Java 8 with Streams and Lambdas. We can now write Java code in the declarative style, rather than imperative. This expresses better the "what", rather than the "how" of the program logic.

Since Java 8, we have had a constant stream of improvements to the Java Programming Language. Records, sealed classes, pattern matching, local variable type inference, and many more. They all serve to make it easier to craft great Java code.

Unfortunately a lot of Java code bases are still stuck in the dark ages of Java 6. This needs refactoring.

"Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior." - Martin Fowler

In this one-day course we learn how and when to "refactor", focusing on the biggest improvement: Java Streams. However, we also show what else is new in the Java Programming Language.

This is a very hands-on course. Each section has exercises where we get to refactor an existing code base of a large ERP system with 330k LOC.

This course includes two live sessions of 4 hours each on the 29th and 30th of November 2021 with Dr Heinz M. Kabutz. Each refactoring has exercises to solve. Heinz shows model solutions and is always happy to answer your questions.

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

  • How to refactor to streams and lambdas with and without IDE assistance
  • The place of streams and lambdas in the history of the JDK
  • What is a functional interface?
  • The four core functional interface types found in the java.util.function package
  • How the lambda notation is a shorthand for an anonymous inner class based on a functional interface
  • The long and short forms that lambdas can take depending on their complexity
  • Method references as a further simplification of certain forms of lambda
  • How default and static methods in interfaces can use lambdas to improve generality, correctness and readability
  • When it might be unsafe to use methods like Map.computeIfAbsent
  • The concept of a stream and its relationship to iterable collections
  • Why coding with streams follows the algorithm logic more naturally than using for/while loops
  • How to create, transform and terminate streams using filters, mappings, matchers, collectors, reducers, etc
  • Why we should use collectors rather than forEach to build collections from a stream
  • Using the Optional class to avoid null checks, and how optionals are used with streams
  • How to handle exceptions in lambdas using sneaky throws (without Lombok)
  • How functional interfaces, streams and optionals are optimized for the primitive types int, long and double

How do these LIVE classes work?

Our LIVE classes consist of two 4-hour sessions. They are highly interactive, with exercises, discussions, and walkthroughs of the solutions. For this course we will record the sessions. They will only be available to students registered for this course. We welcome questions at any time during the live session.

Each 4-hour session runs from 9am-1pm Eastern Time (that is, 2-6pm London, 3-7pm Frankfurt).

Once you enrol in this course, we will sign you up for the webinar. Our system will send you login details. These are personal to you so please do not share them (otherwise you might lose access to the course).


Course Outline

0: Introduction

  • Welcome to the course and how to get the most from your learning
  • Refactoring
  • Inspecting Code with IntelliJ IDEA
  • Java Language Changes

1: Default Methods in Interfaces

  • Default Methods in Interfaces
  • Exercise 1: Replace with List.sort
  • Static Methods in Interfaces
  • Comparator.comparing
  • Functional Interfaces

2: Lambdas

  • Converting an Anonymous Type to Lambda Syntax
  • Statement vs Expression Lambda
  • Exercise: Replace anonymous type with lambda

3: Method References

  • Lambda patterns
  • Exercise: Replace lambda with method reference

4: Iterable and Map forEach()

  • Iterating over Collections and Maps
  • Exercise: Replace loop with forEach()

5: removeIf()

  • Performance gotchas
  • Exercise: Replace loop with removeIf()

6: Map Compound Methods

  • Common coding patterns with Maps
  • Exercise: Replace with Compound Map Methods

7: Stream.all/any/noneMatch()

  • Inspecting a stream for matching elements
  • Exercise: Replace with all/any/noneMatch

8: and collect()

  • Converting streams to maps and collections
  • Exercise: Replace with Map.collect()

9: Collectors.toCollection()

  • Converting streams to specific collection types
  • Exercise: Replace with map() and Collectors.toCollection()

10: Stream.filter()

  • Removing elements from a stream
  • Exercise: Replace with map(), filter(), collect()

11: Collectors.toMap()

  • Converting elements
  • Exercise: Replace with stream(), collect(), Collectors.toMap()

12: Stream.reduce()

  • Reducing a stream to a single value
  • Exercise: Replace with stream(), map(), reduce()

13: Stream.flatMap()

  • Flattening streams of streams
  • Exercise: Replace with flatMap()

14: Optional, findFirst(), findAny()

  • Searching for elements and what to do when none are found
  • Exercise: Replace with findFirst() or findAny()

15: groupingBy(), mapping()

  • Grouping elements into maps
  • Exercise: Replace with collect(), groupingBy() and mapping()

16: Checked Exceptions

  • Handling checked exceptions with sneaky throw
  • Exercise: Handling checked exceptions with ThrowingFunction

17: Conclusion

  • Course wrap-up and next steps


  • This training is for all Java programmers wishing to embrace the Java 8 streams and lambdas coding style
  • Students should download and install the exercises found in the Resources chapter of the course material

Recommended Reading

  • "Mastering Lambdas" by Maurice Naftalin
  • "Refactoring: Improving the Design of Existing Code" by Martin Fowler (first edition uses Java, second uses JavaScript)

Get started now!

Your Instructor

Dr Heinz M. Kabutz
Dr Heinz M. Kabutz

Heinz Kabutz is the author of The Java Specialists’ Newsletter, a publication enjoyed by tens of thousands of Java experts in over 150 countries. He has been a Java Champion since 2005.

Frequently Asked Questions

How long do I have access to a fully paid course?
How does lifetime access sound? After enrolling, you have unlimited access to this course for as long as you like - across any and all devices you own. Furthermore, if we move to another platform, we will offer you a free transfer of your account for all the courses that you have purchased.
How many times can I cancel and rejoin a subscription course?
To prevent abuse, we limit the number of times that you can cancel and rejoin our subscription based courses to three times.
What is the difference between a subscription, paying in installments and an outright purchase?
With a subscription, you never own the rights to the material. If your card is declined or you cancel your subscription, you lose access to the course. The money you have paid so far is lost. Paying in installments is a bit better. You pay for 5 or 10 months and then once you have completed your installments you have lifetime access to the course. The safest is an outright purchase, where you pay the entire course in one amount. There is no risk of losing access.
Can I pay via PayPal?
Yes, you can for outright purchases, but not for recurring payments such as paying by installments or subscriptions.
Can I get an EU VAT Invoice?
Absolutely. First off, be sure to enter the VAT number in the appropriate field during the payment process. Then contact us for an EU VAT Invoice. Please tell us the receipt number for which you need the invoice.
May I share my login details with my colleagues?
Unfortunately not. The terms of usage are for a single license. Teachable tracks your progress through the curriculum, so you won't know how much you have watched. We offer a 30% discount on 50 licenses or more by one company. Please contact us for bulk licensing.
May we use the course for running in-house courses?
You may, as long as each of the students in the class has a valid license for that course. For example, some companies run lunch time Design Patterns study groups using our material. This is an effective way to learn. Please contact us for bulk licensing.
What if I am unhappy with the course?
We would never want you to be unhappy! If you are unsatisfied with your purchase, contact us in the first 30 days and we will give you a full refund and deregister you from the course.
When does the course start and finish?
The course starts now and never ends! It is a completely self-paced online course - you decide when you start and when you finish. We do recommend putting time aside and setting goals to complete the course.
Can I watch the course offline during my commute?
Teachable have an iOS app that lets you watch offline. Android is not supported unfortunately.