Chapter 9. Generic data structures


This chapter covers

  • Separating independent concerns
  • Using generic data structures for data layout
  • Traversing any data structure
  • Setting up a data processing pipeline

We’ll start our discussion of generic types by covering a common case in which they should be used: making independent, reusable components. We’ll look at a couple of scenarios in which we would benefit from an identity function (a function that simply returns its argument) and see a generic implementation of such a function. We’ll also review the Optional<T> type we built in chapter 3 as another simple but powerful generic type.

Next, we’ll talk about data structures. Data structures give shape to our data without having to be aware of what the data is. Making these structures generic allows us to reuse the shape for all sorts of values, significantly reducing the amount of code we need to write. We’ll start with a binary tree of numbers and a linked list of strings, and derive a generic binary tree and linked list from them.

9.1. Decoupling concerns

9.2. Generic data layout

9.3. Traversing any data structure

9.4. Streaming data


Answers to exercises