1 00:00:00,350 --> 00:00:04,640 Sets are another type of data structure that do not preserve order. 2 00:00:04,640 --> 00:00:08,160 Unlike tuples, sets do not allow duplicates. 3 00:00:08,160 --> 00:00:12,905 Each element's order is determined by the value returned from the hash code method. 4 00:00:12,905 --> 00:00:14,630 Sets are immutable by default. 5 00:00:15,910 --> 00:00:19,497 Let's start by cleaning up some of our previous code associated to maps 6 00:00:19,497 --> 00:00:20,167 and tuples. 7 00:00:23,127 --> 00:00:25,955 Awesome, let's create a set of integers from one to five. 8 00:00:32,919 --> 00:00:37,530 In this example, we insert each value in the set in increasing order. 9 00:00:37,530 --> 00:00:40,740 However, if we print out the values of the set, 10 00:00:40,740 --> 00:00:44,370 the first thing we'll notice is that the order is not preserved. 11 00:00:44,370 --> 00:00:44,870 Let's take a look. 12 00:00:50,008 --> 00:00:52,630 Let's recompile and check out our results. 13 00:00:54,940 --> 00:01:00,070 If we really want to maintain the order that each element was inserted in the set, 14 00:01:00,070 --> 00:01:04,870 we can use a LinkedHashSet, which implements a linked list under the hood. 15 00:01:04,870 --> 00:01:10,960 To do so, we will need to import the scala.collection.mutable LinkedHashSet. 16 00:01:10,960 --> 00:01:14,509 Let's do so at the top of our file and create a new LinkedHashSet. 17 00:01:48,061 --> 00:01:50,948 Awesome, let's recompile our app and check out our results. 18 00:01:52,526 --> 00:01:57,420 Perfect, we've created an orderedSet that maintains the order of our elements. 19 00:01:57,420 --> 00:01:59,125 In order to create a mutable set, 20 00:01:59,125 --> 00:02:03,130 we need to import the scala.collection.mutable set. 21 00:02:03,130 --> 00:02:06,405 Let's do so at the top of our file and create a new mutable set. 22 00:02:27,093 --> 00:02:31,806 Similarly to other collections to add or remove elements from a set, 23 00:02:31,806 --> 00:02:36,150 we would simply use the plus equal or minus equal methods. 24 00:02:36,150 --> 00:02:36,650 Let's take a look. 25 00:02:48,879 --> 00:02:51,356 Great, let's recompile and check out our results. 26 00:02:53,798 --> 00:02:58,103 Awesome, as expected, we were able to remove the number 5 from our set, and 27 00:02:58,103 --> 00:03:00,235 we're also able to add the number 4. 28 00:03:00,235 --> 00:03:04,048 We can achieve some very useful functionality with sets, and 29 00:03:04,048 --> 00:03:07,217 that's by using the methods intersect and union. 30 00:03:07,217 --> 00:03:12,503 Intersect grabs the values which are contained in both sets, 31 00:03:12,503 --> 00:03:17,100 while union adds each unique value in both sets. 32 00:03:17,100 --> 00:03:18,093 Let's take a look at how those work. 33 00:03:53,076 --> 00:03:56,007 Awesome, as we can see, we have a set for 34 00:03:56,007 --> 00:03:59,840 the unionResult and a set for the intersectResult. 35 00:03:59,840 --> 00:04:02,440 Let's dissect the intersectResult. 36 00:04:02,440 --> 00:04:06,540 Remember, intersect just means that we grab the values that are contained in both 37 00:04:06,540 --> 00:04:10,350 sets and here, we get the number 4. 38 00:04:10,350 --> 00:04:16,150 That's because both sets, nums as well as numbers, both contain the number 4. 39 00:04:16,150 --> 00:04:19,730 Great, we've gone through quite a few collections in Scala, and 40 00:04:19,730 --> 00:04:24,550 we've learned a lot about Scala's mutable and immutable data structures. 41 00:04:24,550 --> 00:04:28,540 In the next videos, we'll learn about high order functions in Scala, and 42 00:04:28,540 --> 00:04:31,610 how to apply some of those functions to Scala collections.