This course will be retired on July 14, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Video Player
00:00
00:00
00:00
- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
In this video we'll learn what Maps are and create our own Map!
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up-
Zaynah Chowdhury
1,019 Points1 Answer
View all discussions for this video
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Hi, I'm Ben, and in this course
we're going to learn about maps.
0:09
In Java a map is a data type that
lets us store key value pairs.
0:13
You may not know it,
0:18
we've already been using key value
pairs with lists and arrays.
0:19
Let's say we have a list
that looks like this.
0:24
It has Waffles at Index 0, Gyros at
Index 1, and Enchiladas at Index 2.
0:27
Another way to look at this would
be as a mapping of keys to values.
0:33
So with a list or an array the keys
are always going to be the same.
0:38
We'll start with zero and
start counting upwards.
0:42
But sometimes we need a little more
flexibility with what we use as keys,
0:46
this is where maps come in.
0:50
With a map instead of being integers,
our keys can be any type we'd like.
0:52
Which means when you create a map you
not only need to specify a type for
0:58
the values, but you also need
to specify a type for the keys.
1:02
It also means that we can turn this list
into something a lot more meaningful.
1:06
By using a map instead we're able to
associate each of these foods with
1:11
a meal instead of just
some arbitrary number.
1:15
Now that we've got a better
idea of what a map is,
1:19
let's jump into some code and
try to make this map.
1:21
Let's create a new project.
1:24
Click through the screens.
1:28
And name the project Maps.
1:32
Once we've got the project,
let's delete the comment.
1:35
And instead let's create
a new map variable.
1:40
And notice that it's an interface
that takes in two type parameters.
1:45
One for the key and one for the value.
1:51
Let's hit Enter to import it.
1:53
Then let's add the angle brackets and
1:57
declare our keys to be strings and
our values to also be strings.
2:00
Then let's name our map meals and
add the equals sign.
2:05
Now, remember,
2:11
since map is an interface we won't be able
to use it to create a new map object.
2:12
Instead we'll need to use a class
that implements the map interface.
2:17
It's just like what we had to do with our
lists except instead of using list and
2:22
array list here we'll be using map and
HashMap.
2:28
So let's set this equal to a new HashMap,
and use Enter to let it
2:32
auto complete, then add the semi colon,
and we're good to go.
2:36
Now that we've got our map we just
need to add our three meals to it.
2:41
To add an entry to a map
we use the put function.
2:46
So on the next line let's
add our breakfast to
2:49
the map with meals.put and
pass in breakfast for
2:54
the key and Waffles for the value.
3:00
Nice.
3:04
Then let's do the same thing for
our lunch and our dinner.
3:05
So meals.put and lunch will be Gyros.
3:09
And meals.put and let's make dinner at
3:19
our Enchiladas Great,
3:24
let's finish up by printing out what's on
our map to make sure everything's working.
3:28
So System.out and pass in meals and
then let's run it
3:33
Perfect, that's exactly
what we put in there.
3:43
But there's still more
to learn about maps.
3:46
In the next video will see how to
pull items back out of the map and
3:49
see what else the map
interface has to offer
3:53
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