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