Check out a free preview of the full Java Fundamentals course:
The "Set" Lesson is part of the full, Java Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Angie briefly overviews data structures and discusses an unordered collection of unique objects called Set. Adding data to a Set using the Set.of method will make the Set immutable.

Get Unlimited Access Now

Transcript from the "Set" Lesson

>> The next section is one of my most favorite topics, data structures, all right? So data structures are used in programming languages to store a collection of objects in such a way that operations can be performed on the collection. Java provides this collections framework, which consists of interfaces, classes, and methods to store and manipulate data.

[00:00:33] So you can think of the collections framework as basically Java's data structures and algorithms. Now there are multiple collections in Java, we'll focus on four of the most common ones, the set, list, queue, and map. So a set is an unordered collection of unique objects. An example use case for a set would be a standard deck of 52 playing cards, where each card would be an element in this collection, and each of them are unique, so there's no duplicates in set.

[00:01:20] All right, so let's make a program, we're gonna work with fruit today. We're going to create a set of fruit. And let's make a new package now. We'll call this one, data_structures. And I'm gonna make one class and we're gonna put different methods in there for each of the collections so that you have everything kinda in one place.

[00:02:00] So we'll call this CollectionsDemo. And I'll make a method called setDemo. Not set, like setter, but set, like the collection. Before we go deep into that, let's just make a main method that we'll call that, otherwise we'll try to run this and nothing will happen. This needs to be static.

[00:02:36] Okay, so we want to make a set of fruits, right? Set is a collection. Let's go ahead and import it. It comes from the java.util package. Set is an interface, right, which means what? We cannot instantiate it. So I'll call this fruits. So we need to use one of its implementing classes.

[00:03:14] Notice here they give you some examples, HashSet, AbstractSet, SortedSet TreeSet, blah, blah, blah. A popular implementation is HashSet, let's just use that one. Okay, and I would need to import that as well. So now we have a new collection that does not contain anything yet. In order to add things to a collection, we can say fruits.add.

[00:03:44] Add takes an object. So collections are a group of objects, right, not primitive data types. If you needed to put a set of primitive data types, how would you do that?
>> So it would be a list or an array?
>> No, if you specifically want one of these collections, a set or something like that, but you needed to put values that are numbers or something like that, how would you go about doing this?

>> The wrapper?
>> The wrapper classes. So remember we talked about wrapper classes and they are the object forms of the primitive data types, since collections can only take objects. If I needed a set of numbers, I would use integers, because that becomes an object versus int, which is the primitive data type.

[00:04:46] All right, so let's go ahead and add apple. And we'll add a banana. So, by default, because I've added a string, that means that this collection holds string values, all right? They can hold any sort of object, but for sets at least, it needs to be the same type of object, all right?

[00:05:23] Add a banana, let's go ahead and add one more for now, maybe a lemon. Okay, I wanna print this so far. So we added an apple, we added a banana, and then we added a lemon. When we, sorry, I need to print something out, right? So let's go ahead and add a print statement.

[00:05:51] To print this, you simply write the collection name and it'll print it out for you. So we'll run this again. And notice it's showing the three fruits but it's not showing them in the order that we added them in. That's because a set is an unordered collection. So if it's important to you to model something where the order matters, set is not a good option for that.

[00:06:20] Because it just puts it in any random order. This order can even change, if I ran this again or something, it might be a totally different order. So it adds it, but it doesn't keep them in order. All right, let's add another apple and see what happens. So I don't have an error.

[00:06:54] If I run this though, notice I don't have two apples either. That's because sets contain unique items. There cannot be duplicate items. Remember the deck of playing cards, right? You don't want two king of hearts in there, right? So you cannot have duplicate items. It won't complain about it.

[00:07:21] It just simply will not add it again.
>> If you care about the specific position of these different objects, Would you want to use some kind of variation on an array?
>> We will use something else. One of the other four we'll talk about, yeah. Some of them allow or are ordered, some of them don't.

[00:07:50] Okay, so let's say that we wanna know if the set contains something. We didn't do all of these ads, we basically got this collection as a parameter or something like that. And so it's come to us and we need to see, hey, does this thing contain something? There is a method that you can use for that.

[00:08:17] So let's just do a print statement. And we wanna see if it contains a lemon. We can say, let's just do contains lemon. And then we'll append this with fruits.contains(), and then we can ask it, do you contain a lemon? And we'll get a Boolean value back. So if I ran this, I see that it says true.

[00:08:53] We do have a lemon in here. What about if I want to remove an element from a collection? I can do that as well. So to remove an element from the collection, we use the collection name and then I can just say remove. And inside of remove, I specify the object I want removed.

[00:09:22] So, I'll actually put this above the check for lemon so we can see that change. So now I've removed the lemon and then I'll ask, is the lemon there? And this time I have false. So I printed the fruits before I remove the lemon, and then I asked about the lemon being there.

[00:09:51] If I need to know the size of the collection, I can say size and then fruits.size. Just call the size method. So again, this collection provides a lot of convenience methods so that you can easily work with the data. So we have 2 after we've removed the lemon.

[00:10:26] So notice here when we add a fruit to this set, we had to do a line for each fruit that we wanted to add. And so this is four lines of code to add those four things. There's another way that you can set the initial data. So let's create another set.

[00:10:49] We'll call this one moreFruit. And we can say set.of. Set.of will allow you to add however many objects to this collection that you want to. So I'll say pear, raisin, and a cherry. So this is nice, it looks really easy. You're probably thinking, well, I'm gonna just do that.

[00:11:22] Be careful, because if you utilize this set.of method, then this set would be immutable, who remembers what immutable means?
>> You can't change it,
>> You cannot change it. So you would not be able to add anything else. You will not be able to remove anything else. In fact, if I tried to add, it's not gonna complain.

[00:11:52] So if I say morefruit.add, let's put a cranberry in there, notice I don't get a compilation error, however, let's go ahead and just say print this. When I run this, Notice it exploded, and it exploded, Right here when I tried to add a fruit to this immutable list, and it says unsupported operation, basically I cannot modify this.

[00:12:31] Same would happen if I did remove. Now I can do the other things, like check if it contains something or things that are not modifying the collection
>> Is that just going off purely off the values, so it has the pear, the raisin, and the cherry can contain nothing else.

[00:12:48] What if you wanted to remove the cherry, add the cranberry in its place, would that be valid?
>> No, you cannot change this list.
>> Okay, so it cannot be changed cuz it cannot be changed, not necessarily cuz there's a certain spot in memory where only a certain value can exist.

>> No.
>> Okay, I'm thinking about those arrays, with a set number of spots in that array.
>> Yeah.
>> And if you try to go over that limit, it doesn't work. But if you were to change and remove something out to free up that space, then you could.

[00:13:20] That's just where my head was going.
>> Okay.