Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Java Data Structures!
You have completed Java Data Structures!
Preview
A `java.util.Set` interface does not allow for duplicates. There is also `SortedSet` which allows for custom sorts and adds new methods for getting different ranges of the items.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Welcome back.
0:00
It's time to get rid of those
duplicate directors in our current list.
0:01
If we were to attempt something
this with our current knowledge,
0:05
it would definitely be doable,
but just be a little icky.
0:08
We could create a new list
named Unique Directors
0:12
and loop over this list of all directors
and start adding them over.
0:16
And each time we're about to add one,
we first check
0:20
if the new unique list already contains
the one we're about to add.
0:22
And if it does, we don't add it.
0:26
And there's got to be a better way, right?
0:28
Well, of course there is.
0:32
Could you imagine running
0:33
something that over thousands
and thousands of pieces of data?
0:34
That would be ridiculous.
0:38
Luckily, there's a collection interface
that represents
0:40
the unique list we're wanting.
0:43
It's called set, and it's used for stuff
just this.
0:45
There are several implementations
of the set interface.
0:49
So let's go
familiarize ourselves with them.
0:51
The first set
0:55
implementation I'd us to take a look at
is HashSet.
0:56
In most programming languages, a hash
1:00
is a way to uniquely identify objects.
1:02
In Java, every object
has a method called hashCode,
1:06
which is a 32-bit signed integer.
1:09
Like almost all methods, it can be overridden.
1:12
When we talk about hashes
here, we're talking about that hash code.
1:15
It's used heavily
in several of these data structures.
1:18
So let's play around in JShell.
1:21
Let's make a new set.
1:23
Again, the interface is on the left,
so set of strings,
1:25
and let's make a set of the meals
I had this week.
1:30
And the implementation is on the right.
1:33
Hash set of strings, parentheses, perfect.
1:35
You add items to a hash set,
just our list, with the add command.
1:40
So let's add what I had this week.
1:45
Tacos on Monday,
1:47
tacos on Tuesday,
1:51
lasagna on Wednesday,
1:56
Thursday I had tacos again,
2:01
and a burrito on Friday.
2:04
Good times, good times.
2:06
If we look at those meals,
you'll see there are only three items: burritos,
2:10
tacos and lasagna. When I was typing
add the first time it went in it was true
2:14
but after that it was false
each time for the tacos.
2:19
So it returns false
if it's not a new entry. Kinda nice.
2:23
Now let's build a couple of lists
so I can show you how sets and lists
2:28
interact.
2:31
Let's build a list of front-end languages.
2:32
So, list of strings, front-end languages,
2:35
and we'll use that handy dandy Arrays
2:40
as list.
2:42
We'll add HTML, CSS,
2:45
and JavaScript.
2:49
Whoops,
I missed an angle bracket over there.
2:56
Let me add that in real quick.
2:59
There we go.
3:07
Okay, let's also make a list of backend
languages.
3:09
List of strings, backend languages,
3:13
arrays as list, and we'll do Java,
3:19
Python,
3:24
and also JavaScript.
3:28
So we have two lists.
3:31
Let's make one more list to rule them all,
and we'll combine those two together.
3:32
So, list of strings, all languages,
3:37
equals new array list of strings,
and these collections
3:41
can take another collection usually
as an argument to the constructors here.
3:45
So let's pass in front-end languages.
3:49
It's going to make a copy of that list,
basically.
3:52
Most of them
also have a method called addAll,
3:56
so I'm going to add all of the back-end
languages
3:59
to that list too.
4:01
Cool.
4:09
If we look at all languages,
we got everything,
4:11
but JavaScript is in here twice.
4:14
Kinda like our directors, huh?
4:16
Sets, much like
4:18
that list, also take another collection
as an argument to the constructor.
4:19
So let's say set of strings,
4:23
unique languages,
4:28
equals a new
4:31
hashSet of strings and we'll pass in that
4:33
all languages list.
4:36
Cool, now
4:44
JavaScript's only in there once,
but you also notice they're unsorted.
4:45
So here's a case where we can
just change the implementation
4:49
to another one and get new functionality.
4:51
If we go back up
and change the implementation
4:55
to tree set,
4:57
The elements are sorted
automatically in alphabetical order.
5:04
They use a tree structure under the hood
which uses
5:08
branching to arrange its elements,
and what's cool is if I add another, say,
5:10
C sharp,
5:14
it's kept in order.
5:23
They have a couple of new features
as well,
5:24
but we need to update the interface.
5:26
Let me show you real quick what it'll say
though if we don't, so you're familiar.
5:29
There's a method called headset
which will get the first chunk
5:33
of things
before the letter J in this example.
5:36
It doesn't know what we're talking about,
but if we come in here
5:40
and change this implementation
to a sorted set,
5:42
and try that again,
5:53
it will show everything
before the letter J.
5:55
There's also a tail set,
which will give you everything after.
5:59
And there's one called subset.
6:05
So let's say somebody searched Java.
6:06
We can say subset Java.
6:09
Then we give again
6:15
what they typed,
and then we can add character.maxValue.
6:16
Basically,
whatever the largest value is afterwards.
6:19
It'll give you everything
that matches the start of that.
6:23
Pretty cool.
6:28
Okay, let's go use this new knowledge
to remove our duplicate directors.
6:30
I'll slash exit out of that and control
6:34
L to clear it out.
6:36
In example
here, let's import java.util.set
6:40
and java.util.hashset.
6:44
And it's good practice
to keep these in alphabetical order
6:50
like this. Let's go above this
6:52
print statement of all directors here
and - well, why don't you give this a try?
6:58
It should only be one line of code
and we just did it with those languages.
7:04
Create a new hash set of strings and pass
in our all directors list here.
7:08
So pause me now and give it a try.
7:13
Did you get it?
7:17
No worries if not.
7:18
We'll just say set of strings
7:19
unique directors
7:23
equals new hash set of strings
7:26
and pass in all directors.
7:32
Let's replace all directors
in this print statement here
7:36
and give it unique directors.
7:38
Save that and let's run it.
7:43
Awesome, we did it!
7:56
I only see one Nolan, one Miyazaki,
and one Spielberg.
7:58
We could change this to a tree set
if we wanted to sort it too.
8:02
We can change this implementation
to Tree Set
8:06
and change the import up here as well.
8:11
Let's save and see.
8:25
All right, they're sorted
8:32
by first name, but that's still a bit
easier to read than it was before.
8:33
Nice job, everyone.
8:37
There,
8:41
we accomplished what we set out to do.
8:42
I wanted to toss
a slight word of warning here,
8:44
that the comparable interface
that we implemented earlier on our movie
8:47
class is used by
those ordered sets for their ordering.
8:51
Returning zero
should only happen for true equality,
8:54
or things will be considered duplicates.
8:58
I just wanted to give you a heads up.
9:00
There are a lot of warnings about this
in the comparable documentation,
9:01
but it's worth pointing out here
9:05
so you're aware
if your sets start behaving wacky.
9:07
Now let's set out to tackle this challenge
and then navigate our way over to Maps.
9:10
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up