13 Working with sets

 

This chapter covers

  • Comparing differences between sets and arrays
  • Creating sets in different ways
  • Using union and intersect operations to locate items in different types of software
  • Understanding tradeoffs between usings sets and search operations

It does not take much time to define a set and show what operations they can be used for. What does take time is developing an understanding or intuition about what sort of problems you can solve with sets.

Many problems related to organizing and locating data can be solved beautifully by utilizing sets and set operations, but that is not always apparent. In this chapter, I will go through what sets are as well as what you can do with them, and then I’ll walk you through various realistic examples showing the power of storing data in sets.

13.1 What kind of problems can sets help solve?

Lots of software requires organizing large amounts of data, including the following:

  • Photo albums
  • Email clients
  • Bug-tracking systems
  • Online shopping
  • Software-development projects
  • Specialist software, like modeling software for geologists

For a long time the most popular way of organizing data was via tree structures. To find an item you would drill down into subcategories until you found what you were looking for. The problem with this approach is that many items can potentially exist underneath multiple subcategories instead of just one.

13.2 What is a set?

13.2.1 Comparing properties of sets and arrays

13.3 How to use set operations

13.4 How to use sets in your code

13.5 Searching for products using set operations

13.5.1 Defining and using enumerations

13.5.2 Creating test data to perform queries on