Chapter 5. Recursion Techniques in Java 8
Recursion is a powerful functional programming technique that lends itself to more elegant and succinct solutions than an iterative approach. Recursion is the technique where a method, either directly or indirectly, calls itself. A lambda expression can also call itself recursively. The discussion in this chapter will focus on method recursion. However, keep in mind that the ideas and concepts apply equally to recursive lambda expressions.
In this chapter, we will:
- Introduce recursion terminology
- Contrast iterative and recursion techniques
- Demonstrate recursive lambda expressions
- Explore common recursion strategies
- Provide guidance as to when to use recursion
Recursion is not new to Java 8. To illustrate recursion using a method, let's examine what is probably the most commonly used problem to illustrate recursion—the factorial problem. It is defined as follows:
f(1) = 1 f(n) = n * f(n-1) where n > 0
The following is an iterative...