O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Java 8: Lambda Expressions and Streams

Video Description

This video is now available as a 2nd edition.

For the revised and expanded version, go to Java 8 Lambda Expressions and Streams, 2nd Edition http://ssl.safaribooksonline.com/video/programming/java/9780134383644.

Java 8: Lambda Expressions and Streams covers the most important new features introduced in Java 8. It is presented by Marty Hall, bestselling author, world-renowned instructor, and president of the training company coreservlets.com. This LiveLesson explains the syntax and usage of Java 8 lambda expressions, shows the prebuilt functions, covers streams thoroughly, and gives examples of the types of applications to which lambdas and streams are well suited.

Lambda expressions are a way of representing “functions”, and Java 8 introduces a number of prebuilt function building blocks as well as a set of function composition methods. Although lambda expressions are not quite real functions, they are close enough for most purposes, and have very succinct syntax that looks like anonymous functions from Scala (minus the type declarations) and are even moderately similar to JavaScript anonymous functions.

Streams are wrappers around arrays, collections, or other data sources that use lambdas pervasively. They support many convenient and high-performance operations that use lambdas, including "map", "reduce", "filter", and "forEach". They also support lazy evaluation, so if you map firstName over a Stream of Employees, filter the ones that start with "P", then choose the first, it really only maps and filters until the first match. Streams can also be made parallel automatically, so that the operations are done in parallel without having to write any explicit multithreading code.

Lambdas don't exactly turn Java into Lisp, and streams don't exactly turn Java SE into Hadoop. Nevertheless, they provide significant new capabilities to Java, and taken together, lambdas and streams result in by far the biggest change in Java programming style since at least 2004, when generics and annotations were added to the language.

Skill Level: Intermediate to advanced

Who Should Take This Course?: Experienced Java developers who need to learn about the new features introduced in Java SE 8

What You Will Learn:

  • How to use the most important features of Java 8: lambda expressions and streams.
  • Technologies that support lambdas: method references, lambda building blocks in the java.util.function package, effectively-final local variables, the @FunctionalInterface annotation, and higher-order functions.
  • Stream methods: forEach, map, filter, reduce, etc.
  • Infinite (unbounded) streams
  • Parallel streams.
  • Lazy evaluation and short-circuit stream operations.
  • Supporting Java 8 features: interfaces with concrete methods and the Optional class.

Course Requirements: This LiveLesson assumes that you have moderate to strong Java experience. It covers only the new features of the language that are added in Java 8. However, no previous experience with functional programming is required.

About the Author:

Marty Hall is the president of coreservlets.com, a training and consulting company focused on Java-related technologies. He is the author of six popular books on Java Enterprise technology from Pearson and Prentice Hall. He has given Java-related training courses in nine foreign countries and dozens of US venues, and has spoken at Java and WWW-related conferences around the world, including five times at JavaOne. Marty is also adjunct faculty in the Johns Hopkins part-time Master's program in Computer Science, where he directs the Java and Web-related concentration areas. More details can be found at http://courses.coreservlets.com/about-instructor.html.

Table of Contents

  1. Introduction
    1. Introduction to Java 8: Lambda Expressions and Streams LiveLessons 00:07:00
  2. Lesson 1: Java 8: Installation, Setup, and Review of Supporting Topics
    1. Learning objectives 00:00:37
    2. 1.1 Getting Started with Java 8 00:02:07
    3. 1.2 Java 8 References 00:01:19
    4. 1.3 Quick Review of Pre-Lambda Handlers 00:08:10
    5. 1.4 Quick Review of Building Generic Methods and Classes 00:03:44
    6. 1.5 Wrap-Up 00:01:23
  3. Lesson 2: Lambda Expressions Part 1 - Basics
    1. Learning objectives 00:01:03
    2. 2.1 Motivation and Overview 00:08:21
    3. 2.2 Lambdas: Most Basic Form 00:06:06
    4. 2.3 Type Inferencing 00:02:32
    5. 2.4 Expression for Body: Implied Return Values 00:03:23
    6. 2.5 Comparing Java and JavaScript Sorting 00:02:00
    7. 2.6 Omitting Parens for One-Arg Lambdas 00:02:30
    8. 2.7 Using Effectively Final Local Variables 00:02:13
    9. 2.8 The @FunctionalInterface Annotation 00:04:25
    10. 2.9 Method References 00:02:34
    11. 2.10 The java.util.function Package 00:04:33
    12. 2.11 Final Example 00:02:20
    13. 2.12 Wrap-Up 00:04:12
  4. Lesson 3: Lambda Expressions Part 2 – Lambda Building Blocks in java.util.function
    1. Learning objectives 00:00:46
    2. 3.1 Lambda Building Blocks in java.util.function: Overview 00:02:03
    3. 3.2 Predicate 00:07:39
    4. 3.3 Function and BiFunction 00:06:24
    5. 3.4 Consumer 00:01:53
    6. 3.5 Supplier 00:02:46
    7. 3.6 BinaryOperator 00:03:41
    8. 3.7 Wrap-Up 00:01:50
  5. Lesson 4: Lambda Expressions Part 3
    1. Learning objectives 00:00:53
    2. 4.1 Variable Scoping 00:02:38
    3. 4.2 Method References: Details 00:06:06
    4. 4.3 Java 8 Interfaces: Default Methods and Static Methods 00:02:52
    5. 4.4 Methods that Return Lambdas 00:13:29
    6. 4.5 Wrap-Up 00:03:26
  6. Lesson 5: Streams Part 1
    1. Learning objectives 00:00:46
    2. 5.1 Overview of Streams 00:04:40
    3. 5.2 Getting Standard Data Structures Into and Out of Streams 00:05:03
    4. 5.3 Core Stream Methods: Overview 00:02:49
    5. 5.4 forEach – Calling a Lambda on Each Element of a Stream 00:04:15
    6. 5.5 map – Transforming a Stream by Passing Each Element through a Function 00:02:51
    7. 5.6 filter – Keeping Only the Elements that Pass a Predicate 00:02:05
    8. 5.7 findFirst – Returning the First Element of a Stream while Short-Circuiting Earlier Operations 00:04:45
    9. 5.8 Lazy Evaluation 00:06:51
    10. 5.9 Wrap-Up 00:01:15
  7. Lesson 6: Streams Part 2
    1. Learning objectives 00:00:45
    2. 6.1 reduce: Combining Stream Elements 00:07:12
    3. 6.2 collect: Fancier Stream Output 00:08:51
    4. 6.3 Operations that Limit the Stream Size: limit, substream 00:02:42
    5. 6.4 Operations that Use Comparisons: sorted, min, max, distinct 00:05:30
    6. 6.5 Operations that Check Matches: allMatch, anyMatch, noneMatch, count 00:01:48
    7. 6.6 Parallel Streams 00:02:58
    8. 6.7 Infinite (Unbounded On-the-Fly) Streams 00:12:23
    9. 6.8 Wrap-Up 00:03:39
  8. Summary
    1. Summary of Java 8: Lambda Expressions and Streams LiveLessons 00:00:49