Saturday, June 14, 2025

Sets

 In plain English, “set” and “group” mean the same thing—but not in math or computer science. In computer science, a “set,” properly, is a collection of things where each thing is unique: {red, blue, yellow, turquoise, white, black, fuchsia}, or {1, 2, 3, 5, 8, 13, 21}—but not {P, E, N, N, S, Y, L, V, A, N, I, A}.


(As you’ll see soon, the last of these is not, in the Java sense, a “Set”.)

Java maintains Sets, and they have a number of use cases.  At this basic level, perhaps the most obvious is transforming a non-unique collection into a unique collection. That may look like, as roundabout as this is (it works):

  1. Make a List of things
  2. Look at one element at a time
  3. If the element is unique, add it to the Set
  4. Once everything is processed, make a List out of the contents of the set, and you’ll have all the original List, but no duplicates anymore

As usual, before we go further, let me put it out there that there exists a ton of great documentation on Sets, if you know where to look.

(In fact, if you look through the documentation, you’ll see that the above-described 4-step process could be just one—make a call to Set.of(), which will create a set out of whatever you pass.)

Sets are intended to behave just as their mathematical cousins would, so in addition to using of() to quickly make a Set out of something else, you should of course know that there is add() to add things one by one (if they can be added; if thery’re there already, they’ll just be ignored), or addAll() to add all contents of a Collection—more on this later, but the most common you’ve seen so far is a List; or remove (or removeAll, again, passing in a Collection); size(), which gives the count of unique elements in the set; or toArray(), which converts the Set into an array of elements of the type of the Set. Of course, just like with Lists, Queues, and Stacks, it’s quite useful in many circumstances to know if a Set is empty or not; naturally enough, the method for this (as has previously been the case) is isEmpty().

Other operations you might want to do include easily combining several sets into one, checking if a particular item is present or not, or deleting everything inside. These are all supported, and the documentation online does a great job of walking you through how to use these methods—how to call them and what to expect back.

We’ll soon get to dealing with graphs (not bar graphs and pie charts, but rather networks of dots and lines), and many of the algorithms we’ll work through on graphs—certain searching algorithms, or path-finding ones, for example—use Sets extensively.

Data structures like Lists, arrays, Maps, Queues, Stacks, and Sets—you already know 6, and soon, you’ll know many more, so go ahead and celebrate!—can be daunting at first. (I know that when I was taking Data Structures, at least in the beginning, it felt like climbing Mount Everest. But there is good news: It does get easier, and you do get better over time! Don’t worry!)

                                                                                                                          

No comments:

Post a Comment

Switch

 Other than if/if-else/if-else if-else and the ternary operator, there is yet another common and important conditional expression in Java th...