Map Basics5:33 with Ben Deitch
In this video we'll go over the basic functions of the Map interface and walk through using them!
System.out.println(lunch + " " + hasLunch + " " + hasGyros + " " + size);
Now that we've got a populated map, let's see how to get the items back out. 0:00 Instead of printing out the whole map, let's just print out what's for dinner. 0:04 To retrieve an item from a map, you just call the get function and 0:08 pass in the key of the item you're looking for. 0:12 So to print out only what's for dinner, we just need to change this meals 0:15 to meals.get, and pass in dinner. 0:19 Then, if we run the app We'll see that tonight we're getting enchiladas. 0:24 In addition to put and 0:32 get, there's four other functions of maps that you should probably know. 0:33 The first of these is the remove function. 0:38 If you ever need to remove a key value pair, just call the remove method and 0:42 pass in the key. 0:46 So if we wanted to remove lunch, we'd call meals.remove, and pass in lunch. 0:48 Also, in case you need it, the remove function does return a value. 0:56 So if we want, we can set this equal to a new string called lunch. 1:01 After the remove function, we've got the contains key, and 1:08 contains value functions, 1:12 which return a Boolean depending on whether the map contains those items. 1:13 Let's create a new Boolean called hasLunch, and 1:20 set it equal to meals .containtsKey, and pass in lunch. 1:25 And on the next line, let's make another new Boolean called hasGyros, 1:33 and set it equal to meals.containtsValue. 1:40 And pass in gyros. 1:46 Finally, the last function we need to know is the size function, 1:51 which returns how many items are in the map. 1:55 Let's create a new int variable named size, and set it equal to meals.size. 1:58 To wrap it all up, let's see the result of all these functions by printing it all out 2:06 on the next line. 2:11 And let's save some time by just copying the print statement from 2:13 the teacher's notes. 2:15 Now, if we run it We can see that when we remove the lunch, 2:21 we removed gyros which means that hasLunch and hasGyros are both false. 2:29 And our map is down to only two entries. 2:34 Awesome. 2:38 Now, before we go, there is a couple of more things you should know about maps. 2:39 The first is that you can't have any duplicate keys. 2:43 If we try to add a new entry with the key of dinner 2:47 And a value of Pudding It's 2:55 just going to overwrite Enchiladas with Pudding. 3:03 We won't get to have two dinners. 3:07 Deleting that. 3:09 The second thing you need to know is that when you're using strings as keys, 3:12 you almost always want to be using constants. 3:17 It turns out that it's actually pretty easy to make a typo. 3:22 So if down here, I accidentally typed diner instead of dinner, 3:26 we wouldn't get what we're looking for. 3:31 And it doesn't even show up as an error. 3:33 So instead of typing out breakfast, lunch, and 3:38 dinner, we should be using constants set to these values. 3:41 Let's declare these constants at the top of our class. 3:46 But before you start typing anything, 3:50 you should know that IntelliJ actually has a shortcut for creating string constants. 3:52 Let's type psfs and hit Enter. 3:58 Then, let's name our first string BREAKFAST, in all caps, 4:02 which is the convention for constants. 4:06 And set it equal to our breakfast string. 4:08 Then, let's do the same thing for lunch and 4:13 dinner, psfs LUNCH set it equal to lunch. 4:17 And psfs DINNER, set it equal to dinner. 4:23 Great! 4:30 Now, instead of using strings for our keys, let's use our constants. 4:31 So we'll take BREAKFAST and put it over to this one. 4:37 Take LUNCH and put it over to this one. 4:41 And this one. 4:47 And this one. 4:50 And we'll take dinner and put it over this dinner and this diner. 4:52 And when we run it, thanks to the constants, we're back to enchiladas for 5:02 dinner. 5:05 With lists, every item we added had to be associated with an index. 5:07 But with maps, we're free to associated our items with whatever we want. 5:12 We can map students to grades, part numbers to prices, or 5:16 even map strings to strings like we did here. 5:20 So the next time you're tasked with creating a collection of objects, 5:23 if you want to specify what kind of key to use, make sure to use a map. 5:27 Until next time. 5:31
You need to sign up for Treehouse in order to download course files.Sign up