Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Sets are another type of data structure that do not preserve order. Unlike tuples, sets do NOT allow duplicates.
New Terms:
Sets -- A set is another data structure which does not contain any duplicates nor preserves order. Each element in a set gets its order from the return value of the hashCode function.
Further Reading:
-
0:00
Sets are another type of data structure that do not preserve order.
-
0:04
Unlike tuples, sets do not allow duplicates.
-
0:08
Each element's order is determined by the value returned from the hash code method.
-
0:12
Sets are immutable by default.
-
0:15
Let's start by cleaning up some of our previous code associated to maps
-
0:19
and tuples.
-
0:23
Awesome, let's create a set of integers from one to five.
-
0:32
In this example, we insert each value in the set in increasing order.
-
0:37
However, if we print out the values of the set,
-
0:40
the first thing we'll notice is that the order is not preserved.
-
0:44
Let's take a look.
-
0:50
Let's recompile and check out our results.
-
0:54
If we really want to maintain the order that each element was inserted in the set,
-
1:00
we can use a LinkedHashSet, which implements a linked list under the hood.
-
1:04
To do so, we will need to import the scala.collection.mutable LinkedHashSet.
-
1:10
Let's do so at the top of our file and create a new LinkedHashSet.
-
1:48
Awesome, let's recompile our app and check out our results.
-
1:52
Perfect, we've created an orderedSet that maintains the order of our elements.
-
1:57
In order to create a mutable set,
-
1:59
we need to import the scala.collection.mutable set.
-
2:03
Let's do so at the top of our file and create a new mutable set.
-
2:27
Similarly to other collections to add or remove elements from a set,
-
2:31
we would simply use the plus equal or minus equal methods.
-
2:36
Let's take a look.
-
2:48
Great, let's recompile and check out our results.
-
2:53
Awesome, as expected, we were able to remove the number 5 from our set, and
-
2:58
we're also able to add the number 4.
-
3:00
We can achieve some very useful functionality with sets, and
-
3:04
that's by using the methods intersect and union.
-
3:07
Intersect grabs the values which are contained in both sets,
-
3:12
while union adds each unique value in both sets.
-
3:17
Let's take a look at how those work.
-
3:53
Awesome, as we can see, we have a set for
-
3:56
the unionResult and a set for the intersectResult.
-
3:59
Let's dissect the intersectResult.
-
4:02
Remember, intersect just means that we grab the values that are contained in both
-
4:06
sets and here, we get the number 4.
-
4:10
That's because both sets, nums as well as numbers, both contain the number 4.
-
4:16
Great, we've gone through quite a few collections in Scala, and
-
4:19
we've learned a lot about Scala's mutable and immutable data structures.
-
4:24
In the next videos, we'll learn about high order functions in Scala, and
-
4:28
how to apply some of those functions to Scala collections.
You need to sign up for Treehouse in order to download course files.
Sign up