In this lesson, we’ll learn another fundamental Kotlin collection known as a set. A set is an un-ordered collection of unique elements. It’s most commonly applicable in programs that work with data sets containing non-duplicate values such as usernames or passwords. Upon creating or adding elements to a set, the set automatically filters out any duplicates and always returns a unique collection.
As you progress through this lesson, you’ll notice that lists and sets are very common in syntax and share a multitude of built-in functions. Their key difference lies in their purpose and behavior in a program. A developer building a program for a subway line’s train stops may choose to work with a list since trains often cross paths, whereas a program that stores bank account information would most likely be utilizing a set.
As we continue exploring other types of collections in Kotlin, keep in mind the similarities and differences between each one you’ve learned. A good developer doesn’t waste time memorizing every syntax in the book, but instead, focuses on understanding what tools are available and how they can be applied to a program.
Instructions
Take a look at the image on the right.
After setting up camp, Codey is exploring the constellations in the night sky. Since each constellation appears only once, a set is the ideal collection type needed to store this data.
Click Next when you’re ready to start learning more about sets.