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
An implementation of `java.util.Map` provides a key/value representation of data. Maps can be used for all sorts of applications, from counting to indexing.
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
Maps provide
a way to match keys to values.
0:01
Every key must have at most one value.
0:04
Now this is a very handy concept,
and it might seem a little confusing
0:07
at first, but I guarantee
you will end up using this,
0:10
and eventually
you'll start thinking in their format.
0:14
Now let's use a couple of maps
to solve a new issue I'm having.
0:17
I love that we have gotten a nice list,
or set I should say, of our directors.
0:20
It's clean and readable.
0:25
But now, my nerdy brain is curious
as to how many of my saved movie
0:27
recommendations were directed
by these people.
0:31
I don't want to go back to the list way
of having their name duplicated, though.
0:35
It's not very presentable. Hmm...
0:38
You know what would be nice?
0:41
Just a simple counter next to their name.
0:44
We could do this with a map.
0:46
We could have the director name be our key
and our counter number be our value.
0:48
Let's go figure out how to achieve this.
0:53
So first,
0:56
as usual, let's get our feet wet
with how these maps work in JShell.
0:57
So when creating a map, you define
what type the key is and what type
1:06
the value is, separated by a comma
in our angle brackets here.
1:10
A common
use case of maps is to be dictionary-like.
1:14
A term on one side
and a definition on the other.
1:17
So let's do that. Let's make acronyms.
1:20
So we'll put the acronym on the left
and then what it stands for on the right.
1:23
So the key and value will both be strings
here.
1:27
Let's name
it acronyms and that'll be a new hash map.
1:31
So the first kind that we'll explore
here are hash maps.
1:34
So you add to hash maps with put.
1:42
So we can say acronyms.put
and we add our key first.
1:44
Let's do Yagni,
1:49
comma, and our value.
1:51
You aren't gonna need it.
1:53
So it takes two parameters,
the key and the value.
1:55
Cool.
2:01
So note how that returned null.
2:02
That's because there was
no value in here before.
2:04
Here, I'll show you.
2:07
So Yagni actually stands for
you ain't gonna need it,
2:08
which is some slang
instead of you aren't gonna need it.
2:11
So now we'll see that it returned
you aren't gonna need it.
2:15
It returned what was in there before,
just something to note there.
2:18
So let's go ahead and let's add some more.
2:22
I feel I've been repeating myself,
but DRY stands
2:24
for Don't Repeat Yourself.
2:27
And I'll add the most ridiculous one
I know.
2:36
We'll say CAPTCHA.
2:38
You know those little tests
you get online sometimes?
2:40
And that's completely automated public
2:43
turing test to tell computers
2:49
and humans apart.
2:54
Aren't acronyms great sometimes?
2:56
Okay, you can check
and see if a map contains a key.
3:02
So let's see if it contains
3:05
goat.
3:10
Acronyms.containsKey goat.
3:10
I believe that's
Michael Jordan's middle name.
3:10
Don't quote me on that one, though.
3:13
And it doesn't.
3:15
So also, let's see what happens if we try
to get that out of there with get.
3:16
This will get us the value of this key.
3:21
So it returns a null if it's not in there.
3:24
Now side note, remember that
you can actually store nulls in this map.
3:26
So the key might have existed and
the value is intentionally set to null.
3:30
So if you do a get and you see that,
it doesn't always mean that the key didn't
3:35
exist before. Just a little side note.
3:39
So let's go ahead and let's add that one
3:42
so it does exist.
3:44
We'll put goat stands for greatest
3:45
of all time.
3:49
Great, so now let's check
3:53
and see if it does contain the key goat.
3:54
And it does, great!
3:58
And you can also remove,
so let's get Yagni out of there.
3:59
You remove it by the key.
4:04
Note that when it's removed,
it also returns the value.
4:09
So it's like you can pop it out that way
and use it if you wanted to.
4:12
So you can also get a set of all the keys.
4:16
So we'll get all the acronyms
4:21
and we use acronyms.keyset.
4:23
Nice.
4:30
And if you need to loop through the key
and the value at the same time,
4:31
you can do that
using a thing called an entry set.
4:34
So we can use a for each loop.
4:37
So that's for,
and we're going to say map.entry.
4:39
We'll produce a map.entry,
which is an inner class.
4:42
It's only ever intended
to be used from within a map context.
4:46
So it was created inside the map class.
4:50
Just know that that's
why this thing is being used
4:52
and that's why it looks a little funny
with the capital E there.
4:55
So we can say acronyms.entrySet.
4:58
Instead of before we said keySet
and just got the key
5:01
so this is going to give us the key
and the value.
5:04
So map
entries have methods getKey and getValue.
5:07
So we'll say system.out.printf
5:12
and we can say %s stands for %s.
5:16
Put a new line after that
5:20
and for each one
5:23
of those we'll say entry.getKey
5:23
and entry.getValue
5:26
and end the for loop.
5:32
Cool, so let's take a quick
look at some other implementations.
5:36
Let's exit this first.
5:39
So here's
5:44
the map interface again and again
this is K and V
5:44
where K stands for the type of key
and V stands for the type of value
5:48
So what we were looking at
first is HashMap.
5:53
This is kind of like the default that you go
to just like we were using ArrayList
5:56
if we were unsure.
The important thing to note here
5:59
is that the class makes no guarantees
as to the order of the map.
6:03
In particular, it doesn't guarantee that
the order will remain constant over time.
6:07
So you can add a bunch
and the order would change.
6:12
So if you didn't want that behavior,
6:14
the one you probably want to take a look
at is this linked hash map here.
6:16
It uses the linked list implementation.
6:20
Basically meaning,
it'll have a predictable iteration order.
6:23
No matter what, it will always be
in the same order that you added them.
6:27
Now that might not be what you want
either.
6:31
Maybe you want something more
the tree set, and look at that,
6:33
we have a tree map over here.
6:37
So the tree map is sorted by the keys
natural sorting order.
6:40
We'll play with that a little bit.
6:44
Let's jump back over to our code here.
6:45
So first things first, let's import
our hash map
6:48
and our map.
6:51
Okay, so let's
7:01
see what the most popular directors
were in my recommendations.
7:02
I'll go over this print statement here.
7:06
So what we'll do is we'll make the key be
the director and the value be the count.
7:08
So we'll make a new map of string
and integer.
7:13
Now notice I used the uppercase integer.
7:17
That's because values must be of type
object,
7:20
and ints, which we were normally
using, is a primitive.
7:23
So integer needs to be used here,
7:26
and what will happen
is it will automatically go in and out of
7:28
being treated an int
using something called autoboxing.
7:31
I'll put a link in the teacher's
notes if you're curious.
7:35
So we'll say
7:38
directorCounts equals a new hashMap,
7:39
and it'll be of types string and integer.
7:43
Okay, so let's loop over our
all directors again,
7:48
because we need the duplicates
back for this.
7:51
So for string director
8:00
in all directors
8:04
So we'll use the capital integer
here again.
8:10
Integer count equals director counts
8:14
dot get director.
8:20
So we're going to see if this key
already exists in our director counts map.
8:24
Now remember, if it doesn't exist,
it's going to return a null.
8:28
So what we'll do is we'll say,
if this is null,
8:32
meaning it didn't find that in our map,
initialize the count to zero.
8:35
And then after that conditional,
we'll increment the count, right?
8:39
Because if it's not in there it'll be zero
and we'll increment it
8:43
to one for the first entry
for that director.
8:46
If it did find that key in the map
it return the value associated with it
8:49
and it'll be stored in count here,
and we'll add 1 to it.
8:53
Then, we're going to put it in the map,
which will either add it or change it.
8:57
Director counts, dot put,
9:01
director as the key, count as the value.
9:04
Awesome.
9:07
Okay, let's print it out
9:08
with that entry set we played with before.
9:09
We can say for map.entry entry
9:13
in director counts dot
9:18
entry set.
9:22
Nice.
9:24
Then we'll say system.out.printf
9:26
percent s
9:33
colon percent d and percent n.
9:34
and we'll drop in our entry.get key
9:38
and finally entry.get value.
9:43
Let's see if we got this.
9:50
And there we have it.
10:00
All of our directors
and their movie counts.
10:01
Awesome a little baby possum!
10:04
Just like before,
if we wanted to sort it by key,
10:08
we can change this import to tree map
10:11
and use it down below.
10:19
Save and let's run it again.
10:27
And there we go.
10:39
Great job, everyone.
10:40
My OCD nerdy brain is pleased now.
10:41
Awesome.
10:45
We've worked our way
through three of the main interfaces
10:46
in the Java Collection
Framework, List, Set, and Map.
10:49
These will be used all over the place
in your Java inventions,
10:53
and the more you learn about them,
10:56
the better you'll be at choosing the right
one for the task at hand.
10:57
Remember, always use the interface
for the type declaration,
11:01
as you can always change
the implementation later if you need to.
11:05
And side note,
11:09
I ended up
11:10
putting
most of these later movies in myself.
11:11
So they're really a recommendation
list for you all now.
11:13
Some of them are really good,
and some are so bad they're good.
11:16
So the next time
you don't know what to watch,
11:20
maybe there's one in here
you haven't seen before.
11:21
Now there's one interface left
that I'd to talk about in this course,
11:24
and that is called queue,
11:27
but we'll just run into it
in the next stage. I'll put it in the queue.
11:29
Great job! This is a ton of information
and you've picked up so many new tricks
11:33
along the way.
11:37
Let's go have some fun putting them
to work after we wrap up this stage's
11:38
code challenge.
11:42
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