9 Generic Data Structures

 

In this chapter:

  • Separating independent concerns
  • Using generic data structures for data layout
  • A common way for traversing any data structure
  • Setting up a data processing pipeline

We’ll start our discussion of generic types by covering a common case when they should be used: making independent, reusable components. We’ll look at a couple of scenarios where 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 them generic allows us to reuse this 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.1.1 A Reusable Identity Function

9.1.2 The Optional Type

9.1.3 Generic Types

9.1.4 Exercises

9.2    Generic Data Layout

9.2.1 Generic Data Structures

9.2.2 What Is a Data Structure?

9.2.3 Exercises

9.3       Traversing Any Data Structure

9.3.1 Using Iterators

9.3.2 Streamlining Iteration Code

9.3.3 Iterators Recap

9.3.4 Exercises

9.4       Streaming Data

9.4.1 Processing Pipelines

9.4.2 Exercises

9.5       Summary

9.6       Answers to Exercises

sitemap