A set is a collection of items without any duplicate elements. Scala has support for various collections. Sets automatically have access to some test methods: contains, apply, and subsetOf.
- [Instructor] Scala has support for various types of collections. One of those collections is called the set. Sets are iterables that contain no duplicate elements. That's what makes them unique. In your exercise files folder, under chapter 03, 03_05, you'll find a file called setExamples.sc. This file is going to be a good starting point for us to examine sets a little more closely. In order to create a new set, we use the keyword Set with a capital S. And then in parenthesis, we identify the elements in that set.
Here, on line two, I'm creating a variable using the val keyword called fruit. And it has the values apple, orange, and banana. Remember, val means that fruit is immutable, cannot be changed. Then I create another set called moreFruit, which has kiwi and pineapple. This one is declared as V-A-R, or var, so it can be changed. Then we have var nums is equal to the set of the numbers one through five. And finally, I added another set called moreNums which is equal to the set 6, 7, 8 and 9.
In Scala, sets automatically have access to some test methods or test functions. They include contains, apply, and subset of. So let's try one. Let's check to see if nums.contains the number five. When I save this, on the right hand side, we'll see our sets and then on line seven, you can see it says that it's true. Because nums does contain five. What's nice about Scala is for the dot contains, that is like the default method that will be invoked.
So if I just did nums and I put in parenthesis a number, let's say three, that is the same thing as dot contains. So I save that and we're still going to get true because it does have three as well. Next, let's combine these two sets. Remember, in Scala, if you combine a string and a number it just finds the common data type so it's okay to mix data types. Do you remember what it will be if I combine a string and a number? If you can picture the hierarchy, you'll remember that strings are on the right hand side under any ref, and that numbers on the left hand side under any val, but they both go up to any.
So let's do that. Let's create a new variable. And we'll call it mixed. And it's equal to fruit and I want to combine it so I can either use a plus plus to put the two sets together or I can even use the union symbol which is just a single line pipe. But I'm going to do plus plus and I'm going to add nums. Now, remember, so when I save this, mixed is going to have a data type of any. And we see that on the right hand side. And we also see the new set. And the new set looks a little different than what you might have expected because it's not in any order or at least it doesn't appear to be in any order.
Sets are automatically implemented as hash sets which makes retrieving members more efficient. But it also means that they're not always in the same order as what they were inserted. All right, so that was a way to combine two sets. We can add individual elements to a set and we can subtract individual elements so let's do nums, minus equals, and we'll subtract the number five. That will remove five from our set. And since nums is declared as var, if I go ahead and print nums right below it, we will be able to see the result with the five removed.
Let me save that. And we can see that the set now has one through four. We can find the intersection of any two sets. Let's see what nums and more nums have in common. I can do nums and moreNums. The intersection is just a single ampersand and when I save this, since nums and moreNums do not have anything in common, the intersection is the empty set. We scroll over and you'll see the empty set. There are many more useful methods but some that I want to point out are a way to get the first element in a set, a way to get the remaining elements without the first element, and finally, a way to check to see if it's empty.
So if I wanted to print out the first element for more fruit, I could do moreFruit H-E-A-D. That gets the first element in the list. Now, let's also do moreFruit.tail. I already mentioned this but when you first learn this, dot tail, you might think it gets the last element but that's not what happens. It gets everything except for the first element. So in order to process all the elements, you can use a recursive call to dot tail, and each time it will allow you to process the first element and then get the remaining ones.
So it actually comes in real handy when you're working with sets. Then we have the is empty, so let's try moreNums.isEmpty. Let me save this and we can see the results on the right hand side. Okay, we can see that moreFruit.head gave me the variable kiwi. We saw that moreFruit.tail, if I scroll over to the right, we can see that it just has pineapple because kiwi was the first element and pineapple was everything else. And then finally, moreNums is not empty.
These are a few of the methods or functions that you can use with sets. There are many more and I strongly suggest that you get used to using these Scala docs. Let me show you this Scala doc for sets. I'm going to switch over to the scala-lang.org to the API for set.html. You can see here it gives you all the information about the set trait. And if I scroll down, we can see that there are abstract value members as well as concrete value members. So these are a list of all the different methods that you can use on a set.
Released
5/19/2017Join Peggy Fisher as she helps get you started with Scala, so you can leverage the unique capabilities it offers. First, learn the basics of type inference, variables, loops, functions, and operators. Then, find out how to read files using a console, perform pattern matching, handle exceptions, and more. Finally, learn how to use classes, fields, methods, and objects.
- Integrating with IDEs
- Scala worksheets
- Scala repl sessions
- Type inference
- Creating variables
- Working with loops
- Higher-order functions
- Scala operators
- Working with decision statements
- Handling exceptions using try or catch
- Working with tuples and arrays
- Classes, fields, and methods
Share this video
Embed this video
Video: Sets in Scala