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

Learning Java Functional Programming

Book Description

Create robust and maintainable Java applications using the functional style of programming

About This Book

  • Explore how you can blend object-oriented and functional programming styles in Java
  • Use lambda expressions to write flexible and succinct code
  • A tutorial that strengthens your fundamentals in functional programming techniques to enhance your applications

Who This Book Is For

If you are a Java developer with object-oriented experience and want to use a functional programming approach in your applications, then this book is for you. All you need to get started is familiarity with basic Java object-oriented programming concepts.

What You Will Learn

  • Use lambda expressions to simplyfy code
  • Use function composition to achieve code fluency
  • Apply streams to simply implementations and achieve parallelism
  • Incorporate recursion to support an application's functionality
  • Provide more robust implementations using Optionals
  • Implement design patterns with less code
  • Refactor object-oriented code to create a functional solution
  • Use debugging and testing techniques specific to functional programs

In Detail

Functional programming is an increasingly popular technology that allows you to simplify many tasks that are often cumbersome and awkward using an object-oriented approach. It is important to understand this approach and know how and when to apply it. Functional programming requires a different mindset, but once mastered it can be very rewarding.

This book simplifies the learning process as a problem is described followed by its implementation using an object-oriented approach and then a solution is provided using appropriate functional programming techniques.

Writing succinct and maintainable code is facilitated by many functional programming techniques including lambda expressions and streams. In this book, you will see numerous examples of how these techniques can be applied starting with an introduction to lambda expressions. Next, you will see how they can replace older approaches and be combined to achieve surprisingly elegant solutions to problems.

This is followed by the investigation of related concepts such as the Optional class and monads, which offer an additional approach to handle problems. Design patterns have been instrumental in solving common problems. You will learn how these are enhanced with functional techniques.

To transition from an object-oriented approach to a functional one, it is useful to have IDE support. IDE tools to refactor, debug, and test functional programs are demonstrated through the chapters. The end of the book brings together many of these functional programming techniques to create a more comprehensive application. You will find this book a very useful resource to learn and apply functional programming techniques in Java.

Style and approach

In this tutorial, each chapter starts with an introduction to the terms and concepts covered in that chapter. It quickly progresses to contrast an object-oriented approach with a functional approach using numerous code examples.

Table of Contents

  1. Learning Java Functional Programming
    1. Table of Contents
    2. Learning Java Functional Programming
    3. Credits
    4. About the Author
    5. About the Reviewers
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers, and more
        1. Why subscribe?
        2. Free access for Packt account holders
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    8. 1. Getting Started with Functional Programming
      1. Aspects of functional programming
        1. Functions
        2. Function composition
        3. Fluent interfaces
        4. Strict versus non-strict evaluation
        5. Persistent data structures
        6. Recursion
        7. Parallelism
        8. Optional and monads
      2. Java 8's support for functional style programming
        1. Lambda expressions
        2. Default methods
        3. Functional interfaces
        4. Method and constructor references
        5. Collections
      3. Summary
    9. 2. Putting the Function in Functional Programming
      1. Lambda expressions usage
      2. Functional programming concepts in Java
        1. High-order functions
        2. Returning a function
        3. First-class functions
        4. The pure function
          1. Support repeated execution
          2. Eliminating dependencies between functions
          3. Supporting lazy evaluation
        5. Referential transparency
        6. Closure in Java
        7. Currying
      3. Lambda expressions revisited
        1. Java 8 type inference
        2. Exception handling in lambda expressions
      4. Functional interfaces revisited
        1. Creating a functional interface
        2. Common functional interfaces
          1. Function-type functional interfaces
          2. Predicate-type functional interfaces
          3. Consumer-type functional interfaces
          4. Supplier-type functional interfaces
          5. Operator-type functional interfaces
      5. Summary
    10. 3. Function Composition and Fluent Interfaces
      1. Introduction to function composition
      2. Creating composite functions prior to Java 8
      3. Creating composite functions in Java 8
        1. Using the Function interface for function composition
          1. Using the Functional interface to supplement methods
          2. Passing instances of the Functional interface
      4. Fluent interfaces
        1. Fluent interfaces in Java 8
        2. Method chaining and cascading
        3. Contrasting method cascading and fluent interfaces
        4. Creating and using fluent interfaces
          1. Using fluent interfaces to hide older interfaces/classes
          2. Using fluent interfaces with the Properties class
          3. Extending fluent interfaces
      5. Default methods and functions
        1. Static default methods
        2. Default methods in Java 8
        3. Multiple inheritance in Java 8
      6. Summary
    11. 4. Streams and the Evaluation of Expressions
      1. The Stream class and its use
        1. Intermediate and terminal methods
      2. Creating streams
        1. Fixed length streams
        2. Infinite streams
          1. Using the iterate method to create an infinite stream
          2. Using the generate method to create an infinite stream
      3. Using the Stream class methods
        1. Filter methods
          1. Using the filter method
          2. Using the skip method
        2. Sorting streams
        3. Mapping methods
          1. Understanding the mapping operation
          2. Implementing the map-reduce paradigm
          3. Using the flatmap method
      4. Lazy and eager evaluation
      5. Stream and concurrent processing
        1. Understanding non-inference
        2. Understanding stateless operations
        3. Understanding side effects
        4. Understanding the ordering
      6. Summary
    12. 5. Recursion Techniques in Java 8
      1. Recursive data structures
      2. Types of recursion
        1. Using direct recursion
        2. Head and tail recursion
      3. Understanding recursion
        1. The Node class
        2. Using head recursion
        3. Using tail recursion
        4. Using the head and tail recursion
        5. Creating a recursive solution based on a formula
        6. Converting an iterative loop to a recursive solution
        7. Merging two lists
        8. Understanding the program stack
        9. Recursive lambda expressions
        10. Common problems found in recursive solutions
          1. Absence of a base case
          2. Using static or instance variables
          3. Using the pre- and post-increment operators
      4. Recursion implementation techniques
        1. Using a wrapper method
        2. Using short circuiting
        3. Tail call optimization
        4. Converting to a tail call
      5. When to use recursion
      6. Recursion and humor
      7. Summary
    13. 6. Optional and Monads
      1. Using the Optional class
        1. Creating Optional instances
        2. Using the Optional class to support return values
        3. Handling missing values
          1. Using the orElse method to get a substitute value
          2. Using the orElseGet method to use a function to get a substitute value
          3. Using the orElseThrow method to throw an exception
        4. Filter and transforming values
          1. Using the Optional class's filter method
          2. Using the Optional class's map method
          3. Optional solution to the Customer problem
        5. Disadvantages of the Optional class
      2. Monads
        1. Monads in Java 8
          1. Using the of method as the unit function
          2. Using the flatMap method
          3. Using the map method
          4. Using the Optional class with strings
          5. Using monads with the Part class
        2. A formal discussion of monads
          1. Associativity
          2. Left identity
          3. Right identity
      3. Summary
    14. 7. Supporting Design Patterns Using Functional Programming
      1. Implementing the execute-around-method pattern
        1. Object-oriented solution to the execute-around-method pattern
        2. Functional solution to the execute-around-method pattern
        3. Using the execute-around-method pattern with a stream
      2. Implementing the factory pattern
        1. Object-oriented solution to the factory pattern
        2. Functional solution to the factory pattern
      3. Implementing the command pattern
        1. Object-oriented solution to the command pattern
        2. Functional solution to the command pattern
      4. Implementing the strategy pattern
        1. Object-oriented solution to strategy pattern
        2. Functional solution to the strategy pattern
        3. Using the Function interface
      5. Implementing the visitor pattern
        1. Object-orient solution to the visitor pattern
        2. Functional solution to the visitor pattern
      6. Implementing the template pattern
        1. Object-oriented solution to the template pattern
        2. Functional solution to the template pattern
      7. Summary
    15. 8. Refactoring, Debugging, and Testing
      1. Refactoring functional code
        1. NetBeans support for refactoring
          1. Converting anonymous inner classes to lambda expressions
          2. Refactoring multiple code instances
          3. Support of other refactoring operations
        2. Eclipse support for refactoring
          1. Converting anonymous inner classes to lambda expressions
          2. Refactoring multiple code instances
          3. Support of other refactoring operations
      2. Debugging lambda expressions
      3. Using the println method to assist debugging
        1. Using the peek method to assist debugging
        2. Debugging lambda expressions using NetBeans
        3. Debugging lambda expressions using Eclipse
        4. Debugging recursive lambda expressions
        5. Debugging parallel streams
      4. Testing functional programs
        1. Testing lambda expressions
          1. Copying the lambda expression
          2. Using a method reference
          3. Reorganizing the test class
        2. Testing exceptions using a fluent style
      5. Summary
    16. 9. Bringing It All Together
      1. Functional Zork
        1. Playing the game
      2. The game's architecture
        1. Understanding the GameElements class
        2. Introducing the Item, Direction, and NPC classes
        3. Implementing the FunctionalZork class
          1. Initializing the game
          2. Initializing the commands
          3. Getting a command from the console
          4. Parsing the command
          5. Executing the command
        4. Implementing the Character class
          1. Implementing the pickup method
          2. Implementing the drop method
          3. Implementing the walk method
          4. Implementing the inventory method
        5. Implementing the Location class
          1. Handling items
          2. Handling NPCs
          3. Handling directions
      3. Summary
      4. Epilogue
    17. Index