You are previewing Java Closures and Lambda.
O'Reilly logo
Java Closures and Lambda

Book Description

Java Closures and Lambda introduces you to significant new changes to the Java language coming out of what is termed Project Lambda. These new changes make their debut in Java 8, and their highlight is the long-awaited support for lambda expressions in the Java language. You’ll learn to write lambda expressions and use them to create functional interfaces and default methods for evolving APIs, among many other uses.

The changes in Java 8 are significant. Syntax and usage of the language are changed considerably with the introduction of closures and lambda expressions. This book takes you through these important changes from introduction to mastery. Through a set of clear examples, you’ll learn to refactor existing code to take advantage of the new language features. You’ll learn what those features can do for you, and when they are best applied. You’ll learn to design and write new code having these important new features in mind from the very beginning.

  • Clearly explains the fantastic benefits resulting from Project Lambda
  • Explains the syntax and IDE support for the new features
  • Shows how to streamline your code by bringing some of the benefits of functional programming to the Java language
  • Illustrates parallelism in closures through Stream and Spliterator objects
  • Explains API evolution by adding methods to existing interfaces without breaking existing interface implementations, a technique addressing potential multiple inheritance issues
  • Table of Contents

    1. Cover
    2. Title
    3. Copyright
    4. Contents at a Glance
    5. Contents
    6. About the Author
    7. About the Technical Reviewer
    8. Acknowledgments
    9. Introduction
    10. Chapter 1: Java 8: It’s a Whole New Java
      1. Java 8 Returns Java to the Forefront
      2. Java Has Had Functional Programming All Along
      3. Java 8 Is Not Just Syntactic Sugar
      4. Is Java 8 a Functional Programming Language?
      5. Enough with the Theory; onto the Implementation!
    11. Chapter 2: Understanding Lambdas in Java 8
      1. Java 8’s Lambda Syntax
        1. Lambdas as Closures
        2. No-Argument and Multi-Argument Lambdas
        3. Lambdas with No Return Value
        4. Lambdas with Complex Bodies
        5. Lambdas with Explicit Typing
        6. Lambdas as Operators
        7. Lambdas as Predicates
        8. Lambdas with Primitive Arguments
      2. Making Methods into Lambdas
        1. Making Static Methods into Lambdas
        2. Making Constructors into Lambdas
        3. Making Instance Methods into Lambdas
        4. Specifying a Method to Be Used Later
      3. Lambdas as Interface Implementations
        1. Default Methods
        2. Static Methods
      4. Functional Interface Helper Methods
        1. Function.identity() and UnaryOperator.identity()
        2. Function.compose and Function.andThen
        3. Consumer.andThen
        4. Predicate.and and Predicate.or
        5. Predicate.isEqual
        6. Predicate.negate
        7. BinaryOperator.minBy and BinaryOperator.maxBy
      5. Lambda Best Practices
        1. Use Interfaces
        2. Use Method References
        3. Define Lambdas Inline
        4. Lambdas Should Always Be Threadsafe
        5. Don’t Use Null
        6. Don’t Release Zalgo
        7. Build Complexity from Simple Parts
        8. Use Types and the Compiler to Your Advantage
    12. Chapter 3: Lambda’s Domain: Collections, Maps, and Streams
      1. Lambdas and Functional Programming
      2. Functional Iteration
      3. Manipulating Collections and Maps with Lambdas
        1. Filtering Collections and Maps
        2. Mapping Collections and Maps
        3. Map Computations
      4. Streams
        1. Stream Creation
        2. Mapping and Filtering Streams
        3. Collecting, Processing, or Reducing Streams
        4. Primitive Streams
      5. Lambda’s Domain in Review
    13. Chapter 4: I/O with Lambdas
      1. Temporary Files and the Hole in the Middle
        1. Exception Handling via Input: Passing in an Exception Handler
        2. Exception Handling via Output: Capturing Execution Results
        3. Consuming Our Temp File Function
      2. Reading All the Lines of Files in a Directory
        1. Complex Stream Processing Using Creative Flattening
        2. Streaming all the Lines of all the Files in a Directory
      3. Summary
    14. Chapter 5: Data Access with Lambdas
      1. Representing the Intermediary Query Results
      2. Printing Out the Results
      3. Mapping the ResultSet to a Stream
        1. Method One: Building a Stream Using the Stream Builder
        2. Method Two: Building a Stream Using Stream.of and Stream.flatMap
        3. Method Three: Building a Stream Using an AbstractSpliterator
        4. Method Four: Building a Stream from an Iterator
      4. Pulling It All Together
    15. Chapter 6: Lambda Concurrency
      1. Lambdas and Classic Java Threading
      2. Lambdas and Executors
        1. Lambdas and the ThreadPoolExecutor
      3. Lambdas and Fork/Join
      4. Stream Parallelism
      5. Conclusion
    16. Chapter 7: Lambdas and Legacy Code
      1. Resources and Exceptions
        1. Handling Resources by Throwing an Unchecked Exception
        2. Handling Resources with an Exception-Handling Caller
        3. Handling Resources Using a Result Object
      2. Bridging Collections and Streams
        1. Bridging Arrays and Streams
      3. Making Interfaces More Lambda Friendly
      4. Implementing Abstract Classes via Lambdas
      5. The Transparent Optional Trick
      6. Developing Backwards
    17. Chapter 8: Lambdas in Java Bytecode
      1. Hello, Bytecode
      2. Method References in Bytecode
      3. Bound Instance Method References in Bytecode
      4. Free Instance Method References in Bytecode
      5. Inline Lambda Definitions and Lambda Lifting
      6. Conclusion
    18. Appendix A: A Tour of Paradigms
      1. Imperative Programming Paradigm
      2. Object-Oriented Programming Paradigm
      3. Post-Functional Programming Paradigm
      4. Other Programming Paradigms
        1. Logical/Mathematical
        2. Homoiconic
        3. Declarative
        4. Reactive
        5. Stack-Based (Concatenative)
        6. Post-Modern
      5. Paradigms, Style, and Post-Functional Programming
    19. Index