1 00:00:12,788 --> 00:00:16,059 This presentation is delivered by the Stanford Center for Professional 2 00:00:16,059 --> 00:00:23,059 Development. 3 00:00:26,689 --> 00:00:29,300 So time for our next topic. 4 00:00:29,300 --> 00:00:32,618 And our next topic is really a little bit of a revisiting of an old topic, kind of an 5 00:00:32,618 --> 00:00:35,859 old friend of ours, and then we're gonna push a little bit further. So remember our 6 00:00:35,859 --> 00:00:38,839 old interface. 7 00:00:38,840 --> 00:00:40,050 8 00:00:40,049 --> 00:00:43,949 I always love it when math books say like "recall" and they have some concept, and I look at 9 00:00:43,950 --> 00:00:45,239 that, and I'm like, 10 00:00:45,238 --> 00:00:49,299 "Oh, recall the interface. Oh, what good times the interface and I had, like we 11 00:00:49,299 --> 00:00:52,948 were holding hands and running through a garden, the interface and I, and I 12 00:00:52,948 --> 00:00:56,998 recall our happy times together." So now's the time to recall the interface. What 13 00:00:56,999 --> 00:01:00,059 was an interface? Right? Last time when we talked about interface, we talked about something 14 00:01:00,058 --> 00:01:04,959 really generic, which was basically - it was a set of methods. 15 00:01:04,959 --> 00:01:09,209 And this was a set of methods that we sort of designate that some sort of 16 00:01:09,209 --> 00:01:11,149 classes actually shares. 17 00:01:11,149 --> 00:01:15,420 So it's a common set of functionality - 18 00:01:15,420 --> 00:01:18,579 common functionality 19 00:01:18,579 --> 00:01:23,879 among a certain set of classes. 20 00:01:23,879 --> 00:01:28,359 And we talked a little bit about how yeah, there's - if you had some notions of 21 00:01:28,359 --> 00:01:31,579 [inaudible] classic standing in other class, you get sort of that same idea, but the 22 00:01:31,579 --> 00:01:34,760 real generality of interface is with - that you could have certain things that weren't 23 00:01:34,760 --> 00:01:38,279 related to each other in an object hierarchy or a class hierarchy 24 00:01:38,278 --> 00:01:41,278 that you still wanted to have some common methodology. And you sort of saw this all 25 00:01:41,278 --> 00:01:45,828 before, so in the days of yore when we talked about G objects. Remember? 26 00:01:45,828 --> 00:01:49,789 Those little fun graphical objects like the GLabel and the GRect, and all that 27 00:01:49,790 --> 00:01:52,960 happy stuff. And we said there were a bunch of interfaces there. Like for example, there was an 28 00:01:52,959 --> 00:01:55,729 interface called 29 00:01:55,730 --> 00:01:59,969 GFillable, and the GFillable interface had certain methods associated with it, like 30 00:01:59,969 --> 00:02:03,679 you could set something to be filled, or you could check to see if it was filled. 31 00:02:03,680 --> 00:02:06,760 And we said some of the objects actually 32 00:02:06,760 --> 00:02:11,310 implemented this interface, so for example, GRect, and GOval, and a couple others 33 00:02:11,310 --> 00:02:13,848 actually implement this GFillable 34 00:02:13,848 --> 00:02:17,168 interface. And there were other things like GLabel where it didn't make sense to 35 00:02:17,169 --> 00:02:17,760 have a 36 00:02:17,759 --> 00:02:22,328 filled or unfilled label, so GLabel didn't implement this interface. Okay? 37 00:02:22,329 --> 00:02:24,659 And it was just kind of a set of functionality. We're gonna kinda 38 00:02:24,658 --> 00:02:26,469 return to this idea of interface 39 00:02:26,469 --> 00:02:28,030 to talk about some of the things 40 00:02:28,030 --> 00:02:31,680 that we've actually done so far, like array lists, and some new concepts 41 00:02:31,680 --> 00:02:34,579 you're gonna learn, and how it relates to our notion of interfaces. 42 00:02:34,579 --> 00:02:38,540 But the more general thing to take away from interfaces is in terms of thinking 43 00:02:38,539 --> 00:02:39,498 about 44 00:02:39,498 --> 00:02:42,688 how they're actually implemented for their syntax. 45 00:02:42,688 --> 00:02:44,409 Sometimes what you'll see 46 00:02:44,409 --> 00:02:45,198 in 47 00:02:45,199 --> 00:02:49,270 the actual code when you see that a particular class implements 48 00:02:49,270 --> 00:02:52,460 the methods that are considered part of some interface, the way we write is that is 49 00:02:52,460 --> 00:02:54,250 we say public class, 50 00:02:54,250 --> 00:02:58,729 and then we have the class name just like we usually do. So I'll write class name here, 51 00:02:58,729 --> 00:03:01,109 and I'll underline it to indicate that this is just 52 00:03:01,110 --> 00:03:04,819 a placeholder for the actual name of the class as opposed to writing class name. 53 00:03:04,818 --> 00:03:08,628 And then what we would write is implement - 54 00:03:08,628 --> 00:03:11,810 this would all generally be on the same line, much in the same way when you've seen before 55 00:03:11,810 --> 00:03:14,688 like your class, my program extends 56 00:03:14,688 --> 00:03:17,039 console program, or extends graphics program. 57 00:03:17,039 --> 00:03:20,679 Here we have a notion of implements, which is an actual word in Java, 58 00:03:20,680 --> 00:03:24,540 and then the name of the interface that it implements. So this over here 59 00:03:24,539 --> 00:03:27,679 would be the interface name. 60 00:03:27,679 --> 00:03:31,838 So you can imagine somewhere in the definition of the GRect class, there is a 61 00:03:31,838 --> 00:03:33,300 GRect class 62 00:03:33,300 --> 00:03:35,530 that implements GFillable. 63 00:03:35,530 --> 00:03:38,979 And GFillable is defined to be some interface somewhere which just specifies a 64 00:03:38,979 --> 00:03:40,189 set of methods, 65 00:03:40,189 --> 00:03:42,899 and any class that implements that interface 66 00:03:42,899 --> 00:03:46,998 needs to provide its own version of the methods. That's all it means, so for a class to 67 00:03:46,998 --> 00:03:48,218 implement some interface 68 00:03:48,218 --> 00:03:52,250 just means that that interface specifies some set of methods, and this class 69 00:03:52,250 --> 00:03:55,009 provides all of those methods. By providing all of those methods, 70 00:03:55,009 --> 00:03:57,908 it what's called implements the interface. Okay? 71 00:03:57,908 --> 00:04:01,568 And it's perfectly fine for a class to not only implement an interface, but also to 72 00:04:01,568 --> 00:04:04,128 extend some other class. That's perfectly fine, so the syntax is gonna 73 00:04:04,128 --> 00:04:05,359 get longer up here, 74 00:04:05,360 --> 00:04:08,009 but I just want you to see the implements 75 00:04:08,008 --> 00:04:11,448 syntax. And then inside here, just like you would be used to - actually, I'll just draw the 76 00:04:11,449 --> 00:04:13,080 opening brace here and inside here - 77 00:04:13,080 --> 00:04:16,470 this would be all of your code for the implementation of the methods would go in there. 78 00:04:16,470 --> 00:04:19,480 So it's the same kind of syntax for writing a regular class, but now 79 00:04:19,480 --> 00:04:22,270 you're just specifically telling the machine, "Hey, this class is gonna 80 00:04:22,269 --> 00:04:24,649 support all the stuff from 81 00:04:24,649 --> 00:04:29,228 some particular or other interface," like GFillable or whatever the case may be. Okay? 82 00:04:29,228 --> 00:04:30,969 So that's kinda the concept. Now 83 00:04:30,970 --> 00:04:35,359 why do we sort of revisit this idea of interface 84 00:04:35,358 --> 00:04:38,928 is that now it's time to talk about some new things and the interfaces that they 85 00:04:38,928 --> 00:04:42,328 actually implement. So any questions about the basic notion of an interface or 86 00:04:42,329 --> 00:04:45,908 implementing an interface? Good 87 00:04:45,908 --> 00:04:49,699 call on the microphone. How's this different from - does 88 00:04:49,699 --> 00:04:54,699 this work? Sure. Just press the button. We'll pretend it's working. How is this different from extending a class or 89 00:04:54,699 --> 00:04:58,639 just calling it an instance of another class? Right. So that's a good question. The 90 00:04:58,639 --> 00:05:00,810 difference between this and extending a class 91 00:05:00,810 --> 00:05:02,530 is for example the notion of 92 00:05:02,529 --> 00:05:06,598 a hierarchy that we talked about. So if we have some class that extends some other 93 00:05:06,598 --> 00:05:07,168 class, 94 00:05:07,168 --> 00:05:08,609 we basically say, right - like 95 00:05:08,610 --> 00:05:12,590 when we talked about - remember in the very first day - primates and all humans are 96 00:05:12,589 --> 00:05:14,008 primates? 97 00:05:14,009 --> 00:05:16,669 We would basically say any human is a primate. 98 00:05:16,668 --> 00:05:19,678 The difference is there might actually be some things that are not primates, and 99 00:05:19,678 --> 00:05:22,339 humans may actually implement a class like 100 00:05:22,339 --> 00:05:26,229 the GIntelligence class, which means you have a brain that's larger than a pea or 101 00:05:26,230 --> 00:05:27,810 something like that. It 102 00:05:27,810 --> 00:05:32,459 turns out there's this other thing over here called the dolphin which also 103 00:05:32,459 --> 00:05:36,000 implements the Intelligence class. Right? We generally like to think of 104 00:05:36,000 --> 00:05:38,939 dolphins as - a point of debate. I don't how they actually measure this. There's like 105 00:05:38,939 --> 00:05:43,988 the dolphin SAT or something, the DSAT, and they're like [inaudible] - 106 00:05:43,988 --> 00:05:47,109 I don't know how they do it, but evidently someone has figured out how to measure 107 00:05:47,110 --> 00:05:51,218 intelligence in dolphins. So you could say this dolphin class actually implements the methods 108 00:05:51,218 --> 00:05:53,959 of being intelligent, which might be some method like 109 00:05:53,959 --> 00:05:56,538 it responds to some stimulus in the world, 110 00:05:56,538 --> 00:05:58,538 but a dolphin's not a primate, 111 00:05:58,538 --> 00:06:00,538 so - at least as far as I know. 112 00:06:00,538 --> 00:06:04,418 Dolphin's not a primate, so there's no extends relationship here, and that's the 113 00:06:04,418 --> 00:06:08,359 difference. Right? So an interface in some sense provides more generality. When you 114 00:06:08,360 --> 00:06:11,739 extend the class, you're saying there's a direct hierarchical relationship among 115 00:06:11,738 --> 00:06:15,998 those objects. Interfaces, you're just saying there's this thing like the 116 00:06:15,999 --> 00:06:18,479 intelligence interface, 117 00:06:18,478 --> 00:06:22,528 and sometimes these get drawn with dash lines when you see them in diagrams. 118 00:06:22,528 --> 00:06:25,728 Both humans and dolphins provide all the methods of the intelligence interface, 119 00:06:25,728 --> 00:06:29,988 but only a human is a primate. That's the difference. Okay? 120 00:06:29,988 --> 00:06:34,578 So any other questions? Uh huh? Is 121 00:06:34,579 --> 00:06:35,720 the code in this 122 00:06:35,720 --> 00:06:39,659 instance the code for that particular class or the 123 00:06:39,658 --> 00:06:42,990 interface? Pardon? You wrote code there as in - 124 00:06:42,990 --> 00:06:47,900 is that the code for that class or for the interface? Yeah. So the code here is the code for 125 00:06:47,899 --> 00:06:51,758 class name, and somewhere else there would actually be the code for the interface. 126 00:06:51,759 --> 00:06:54,718 And that's covered in the book. We're not actually gonna be writing an interface together, 127 00:06:54,718 --> 00:06:58,329 which is why I'm not showing that to you here in detail, but the basic idea is 128 00:06:58,329 --> 00:07:01,559 just so that you would actually see that when a class implements an interface what that syntax 129 00:07:01,559 --> 00:07:04,199 would look like. Uh huh? Can you 130 00:07:04,199 --> 00:07:05,669 implement more than one interface? 131 00:07:05,668 --> 00:07:08,758 Yes. You can implement multiple interface. 132 00:07:08,759 --> 00:07:12,809 For example, GRect implements both the GFillable interface and the 133 00:07:12,809 --> 00:07:16,009 GResizable interface, and that's perfectly fine. 134 00:07:16,009 --> 00:07:17,189 It's a good time. All 135 00:07:17,189 --> 00:07:20,298 right, so let me move on a little bit and talk about 136 00:07:20,298 --> 00:07:23,188 something that we're actually gonna deal with which is an interface, 137 00:07:23,189 --> 00:07:26,259 which is why we kinda revisit the subject. 138 00:07:26,259 --> 00:07:28,588 And the particular thing we're gonna talk about 139 00:07:28,588 --> 00:07:31,899 is something that's known as a map. Okay? 140 00:07:31,899 --> 00:07:33,068 So a map - 141 00:07:33,069 --> 00:07:37,319 and you might look at this, and you're like, "Map? Is that like oh yeah, the travel 142 00:07:37,319 --> 00:07:40,699 guide, like I got one of these, and I kinda look in here, and I'm like yeah, 143 00:07:40,699 --> 00:07:42,250 where was I going? 144 00:07:42,250 --> 00:07:46,480 Oh, here's a map of all the interstates on the United States, and I'm sure this is copyright, 145 00:07:46,480 --> 00:07:48,770 mapquest.com. 146 00:07:48,769 --> 00:07:51,198 There's this map, right? Is this what you're talking about?" 147 00:07:51,199 --> 00:07:55,009 No. This is not at all to do with what I'm referring to here as maps. So 148 00:07:55,009 --> 00:07:55,559 149 00:07:55,559 --> 00:07:58,580 when you think of the word map, you need to let go of what your previous notion 150 00:07:58,579 --> 00:08:00,418 of what a map might have been is, 151 00:08:00,418 --> 00:08:03,088 and think of the computer science notion of a map. 152 00:08:03,088 --> 00:08:06,038 So basically, all a map is - 153 00:08:06,038 --> 00:08:08,028 it's an interface in Java, 154 00:08:08,028 --> 00:08:10,930 which means it's some collection of methods that will implement some 155 00:08:10,930 --> 00:08:11,968 functionality. 156 00:08:11,968 --> 00:08:15,569 What are the methods? What is a actual map? What does it do? 157 00:08:15,569 --> 00:08:18,979 The way to think about a map is there's two key terms you wanna think about 158 00:08:18,978 --> 00:08:23,269 for map. One is called the key, and one is called the value. 159 00:08:23,269 --> 00:08:28,468 And basically, all a map is is it's a way of associating a particular key with 160 00:08:28,468 --> 00:08:30,110 a particular value. 161 00:08:30,110 --> 00:08:33,639 You might say, "Whoa, man. That's kinda weird." Yeah, that's a very abstract idea. So let 162 00:08:33,639 --> 00:08:37,278 me give you an example of a map that you've probably used throughout the majority of 163 00:08:37,278 --> 00:08:38,129 your life, 164 00:08:38,129 --> 00:08:41,610 but no one told you up until now that it was map, 165 00:08:41,610 --> 00:08:43,440 something called the dictionary. 166 00:08:43,440 --> 00:08:46,040 Anyone ever use the dictionary? 167 00:08:46,039 --> 00:08:49,339 The number's getting smaller and smaller. No, I just let my word processor spell 168 00:08:49,340 --> 00:08:50,800 correct for me. 169 00:08:50,799 --> 00:08:52,889 A dictionary is a map. 170 00:08:52,889 --> 00:08:57,899 Its keys are the words that exist in the dictionary, and the values 171 00:08:57,899 --> 00:09:02,340 are the definitions of those words. 172 00:09:02,340 --> 00:09:06,410 So what a dictionary gives you is the ability to associate a particular key 173 00:09:06,409 --> 00:09:07,318 with 174 00:09:07,318 --> 00:09:10,059 a value. It associates words with their definitions. 175 00:09:10,059 --> 00:09:13,559 And the important idea to think about a map is in a map 176 00:09:13,559 --> 00:09:15,000 what you do is you add 177 00:09:15,000 --> 00:09:18,028 key value pairs - so like in a dictionary like the Oxford English 178 00:09:18,028 --> 00:09:20,879 Dictionary, right every year there's a little convention, and they actually figure out some 179 00:09:20,879 --> 00:09:23,149 new words that are considered part of English, 180 00:09:23,149 --> 00:09:27,119 and they add them to the map that is the OED. Anyone have a copy of 181 00:09:27,119 --> 00:09:28,009 182 00:09:28,009 --> 00:09:31,830 the OED? Yeah, a couple folks. It's a good time. Just get it. You get the little magnifying glass version where it's 183 00:09:31,830 --> 00:09:35,879 got like four pages on one page, hurts your eyes, but it's a good time. 184 00:09:35,879 --> 00:09:39,309 That's what it is. We're just adding key value pairs. Now what you do in a dictionary is 185 00:09:39,309 --> 00:09:43,089 when you look things up in a map, you don't look them up by the value. Right? It 186 00:09:43,090 --> 00:09:44,930 would make no sense if I told you 187 00:09:44,929 --> 00:09:48,778 look up the word for me that has this as its definition. You'd kind of 188 00:09:48,778 --> 00:09:51,210 be like, "Yeah, Mehran. That's just cruel and unusual." 189 00:09:51,210 --> 00:09:53,009 The way a map works is 190 00:09:53,009 --> 00:09:56,810 you look things up by their key. You have a particular word in mind in a dictionary, 191 00:09:56,809 --> 00:10:00,078 you go to the dictionary, you look up that word, and then you get its corresponding 192 00:10:00,078 --> 00:10:01,279 definition. Okay? 193 00:10:01,279 --> 00:10:05,428 So what we like to think of a map in this abstract way it's an association. 194 00:10:05,428 --> 00:10:08,720 It's an association of a key to a value 195 00:10:08,720 --> 00:10:11,959 where when we enter things we enter both the key and the value together, and 196 00:10:11,958 --> 00:10:13,209 when we look things up, 197 00:10:13,210 --> 00:10:18,150 we look things up by saying get me the value associated with this key. Okay? 198 00:10:18,149 --> 00:10:23,559 So dictionary's a simple example of that. There's other examples. Your phone book 199 00:10:23,559 --> 00:10:25,639 is same thing. It's a map. 200 00:10:25,639 --> 00:10:28,789 The keys in the case of your phone book happen to be names that you wanna look 201 00:10:28,789 --> 00:10:29,409 up, 202 00:10:29,409 --> 00:10:33,179 and the values in the case of your phone book happen to be phone numbers, but it's 203 00:10:33,179 --> 00:10:34,599 also a map. 204 00:10:34,600 --> 00:10:37,720 There's maps kind of all around you. Everywhere in life, there's a whole bunch 205 00:10:37,720 --> 00:10:40,189 of maps. No one told you they were maps before. 206 00:10:40,188 --> 00:10:43,480 And if I told you all before this class started, 207 00:10:43,480 --> 00:10:46,129 "Yeah, a dictionary and a phone book are really the same thing," you might kinda 208 00:10:46,129 --> 00:10:50,269 look at me sorta weird and be like, "No, Mehran. One is like storing 209 00:10:50,269 --> 00:10:53,500 names and numbers, and the other one's storing words and definitions." But when 210 00:10:53,500 --> 00:10:56,590 you tell this to a computer scientist, they're like, "Yeah. They're both maps," 211 00:10:56,590 --> 00:10:58,149 because now you know what a map is. 212 00:10:58,149 --> 00:11:04,350 All right. So any questions about the general idea of a map? Uh huh? [Inaudible]. 213 00:11:04,350 --> 00:11:07,889 As far as we're dealing with right now, you can think of a key having just one 214 00:11:07,889 --> 00:11:10,929 value, but that value is really some abstract notion, right? 215 00:11:10,929 --> 00:11:13,128 So you could actually think we have some 216 00:11:13,129 --> 00:11:16,550 word - like let's say we have a dictionary that has multiple definitions. 217 00:11:16,549 --> 00:11:20,108 Its key could be a word. Its value could be an array of 218 00:11:20,109 --> 00:11:21,639 definitions. So 219 00:11:21,639 --> 00:11:25,159 really, in the philosophical sense the value is one thing. That one thing 220 00:11:25,159 --> 00:11:28,219 happens to be an array that stores multiple values. 221 00:11:28,220 --> 00:11:31,490 So philosophically it's one thing. In terms of the practicality of what you're storing, 222 00:11:31,490 --> 00:11:35,169 you might actually be storing multiple things. And oh, maybe for Assignment No. 6, you might 223 00:11:35,169 --> 00:11:37,379 actually do something like that, but that's not important right now because you're still 224 00:11:37,379 --> 00:11:40,149 working on Assignment No. 5. All right? 225 00:11:40,149 --> 00:11:44,059 So [inaudible] something, you're like, "Quick. Write that down." 226 00:11:44,059 --> 00:11:47,500 Yeah, we'll talk about it again when we get to Assignment No. 6. All right? 227 00:11:47,500 --> 00:11:49,029 So 228 00:11:49,029 --> 00:11:52,399 map is a particular interface, so that means we need to define some class that 229 00:11:52,399 --> 00:11:57,000 actually implements this interface, that provides some notions of these key value 230 00:11:57,000 --> 00:11:57,948 abstractions for us, 231 00:11:57,948 --> 00:12:02,609 and so in Java we have something called a hashmap. Okay? 232 00:12:02,610 --> 00:12:08,090 A hashmap is an actual class. It is a class that implements the map interface, 233 00:12:08,090 --> 00:12:11,740 which means everything that is a hashmap 234 00:12:11,740 --> 00:12:16,720 implements map. Okay? 235 00:12:16,720 --> 00:12:19,790 Map is not actually a class in itself. 236 00:12:19,789 --> 00:12:24,049 Hashmap is a particular class. Map is just a set of methods that I think are 237 00:12:24,049 --> 00:12:25,589 actually useful to have. 238 00:12:25,590 --> 00:12:29,490 Now where did it get its name? Why do we call it a hashmap? What's this hashing thing all about? And in the 239 00:12:29,490 --> 00:12:32,899 book in excruciating detail, it talks about hashing, what hashing is, 240 00:12:32,899 --> 00:12:35,970 and how to implement hashing, and hash codes, and all this other stuff. 241 00:12:35,970 --> 00:12:38,360 You don't need to know any of that for the class. 242 00:12:38,360 --> 00:12:42,000 All you need to know is how to use a hashmap. You don't need to build a hashmap. 243 00:12:42,000 --> 00:12:42,429 244 00:12:42,429 --> 00:12:45,958 If you wanna build a hashmap, in CS106B a couple weeks from now, 245 00:12:45,958 --> 00:12:50,078 you will build a hashmap. You will have the joy of seeing the underlying 246 00:12:50,078 --> 00:12:53,339 implementation of hashmaps in C++ of all things. 247 00:12:53,340 --> 00:12:55,759 But for right now, you just don't need to worry about it. The reason why it's 248 00:12:55,759 --> 00:12:59,870 called a hashmap is it uses a particular strategy to store these key value pairs 249 00:12:59,870 --> 00:13:01,009 called hashing. 250 00:13:01,009 --> 00:13:03,419 That's all you need to know, and because it's called hashing, we call it a hashmap. 251 00:13:03,419 --> 00:13:05,349 That's life in the city. Okay? 252 00:13:05,350 --> 00:13:08,000 But so this particular hashmap 253 00:13:08,000 --> 00:13:09,679 is a template. 254 00:13:09,679 --> 00:13:12,620 Right? Remember we talked about templates, and we talked about how the array list 255 00:13:12,620 --> 00:13:15,409 was a template, and you could have an array list of strings, or you could have an 256 00:13:15,409 --> 00:13:18,029 array list of integers, or whatever the case may be. 257 00:13:18,029 --> 00:13:23,509 A hashmap is also a template, but the key here is there are two 258 00:13:23,509 --> 00:13:25,159 types involved. It's like 259 00:13:25,159 --> 00:13:29,569 two scoops of raisins. You have two types involved in the template for 260 00:13:29,570 --> 00:13:30,160 a hashmap. 261 00:13:30,159 --> 00:13:33,069 You're gonna have a type for your keys, and you're gonna have a type 262 00:13:33,070 --> 00:13:34,320 for your values. 263 00:13:34,320 --> 00:13:37,810 So what does that actually look like in terms of syntax? Let me create a hashmap for 264 00:13:37,809 --> 00:13:41,149 you. 265 00:13:41,149 --> 00:13:44,439 Now that you know all about interfaces, we can erase this part. 266 00:13:44,440 --> 00:13:48,600 So let's create a hashmap. The class is called hashmap. 267 00:13:48,600 --> 00:13:52,670 Because it's a template, it has this funky angled bracket notation, 268 00:13:52,669 --> 00:13:56,110 but it's gonna have two parameters for two types 269 00:13:56,110 --> 00:13:57,710 for this templatized class. 270 00:13:57,710 --> 00:14:01,389 The first type is what are you gonna have for your keys. So let's say we wanna 271 00:14:01,389 --> 00:14:05,799 implement a dictionary. A dictionary for its key type is gonna have strings 272 00:14:05,799 --> 00:14:08,389 because it's gonna be words. 273 00:14:08,389 --> 00:14:11,740 What type - the next type - so you have a comma in here, and then you specify the 274 00:14:11,740 --> 00:14:13,959 next type, the type for your value. 275 00:14:13,958 --> 00:14:16,778 For a dictionary again, if we assume a simple 276 00:14:16,778 --> 00:14:19,539 definition rather than multiple definitions which could be an array, 277 00:14:19,539 --> 00:14:23,860 we'll also just gonna have a string for the value type. 278 00:14:23,860 --> 00:14:28,460 So hashmap string comma string, or sometimes we refer to this as a map 279 00:14:28,460 --> 00:14:33,190 from strings to strings because it maps from keys to values 280 00:14:33,190 --> 00:14:36,470 is how we would actually specify this. We need to give it some name, so we might 281 00:14:36,470 --> 00:14:39,310 call this "dict" for dictionary, 282 00:14:39,309 --> 00:14:43,828 and then this is going to - we are gonna create one by saying a new hashmap - and 283 00:14:43,828 --> 00:14:47,859 this is where the syntax gets a little bit bulky, but you just have to stick with it - 284 00:14:47,860 --> 00:14:53,629 string, string, and we're calling a constructor, so again we have the open paren, close paren. 285 00:14:53,629 --> 00:14:57,870 And that will create for you a hashmap that maps from strings to strings. 286 00:14:57,870 --> 00:15:03,019 Now we could also create a hashmap for a phonebook, so let's create a hashmap for a phonebook. 287 00:15:03,019 --> 00:15:06,199 In the case of a phonebook, you might say, "Hey, 288 00:15:06,200 --> 00:15:09,920 names are still gonna be strings, but phone numbers" - well, phone numbers if they're 289 00:15:09,919 --> 00:15:13,059 generally seven digit phone numbers - let's just say it's seven digit phone numbers. We won't 290 00:15:13,059 --> 00:15:15,479 worry about the ten digit phone numbers. 291 00:15:15,480 --> 00:15:18,389 Seven digit phone numbers, I can store that in an integer. 292 00:15:18,389 --> 00:15:24,830 And so you might say, "Hey, I'm gonna have a hashmap of string to int." And at this 293 00:15:24,830 --> 00:15:27,550 point you should stop 294 00:15:27,549 --> 00:15:30,139 because your compiler will give you an error. 295 00:15:30,139 --> 00:15:33,879 What's the problem with this? [Inaudible]. 296 00:15:33,879 --> 00:15:35,129 Int is a primitive. 297 00:15:35,129 --> 00:15:39,120 Right? All these templative types require objects for their types. They 298 00:15:39,120 --> 00:15:42,230 require classes, which means we can't give it a primitive. We have to give it 299 00:15:42,230 --> 00:15:44,720 the wrapper class integer. 300 00:15:44,720 --> 00:15:48,600 So it's gonna be a map from strings to integer, 301 00:15:48,600 --> 00:15:49,479 302 00:15:49,479 --> 00:15:51,509 and we'll call this thing 303 00:15:51,509 --> 00:15:53,879 phone book. 304 00:15:53,879 --> 00:15:57,889 And this is just going to be - we'll give you the syntax in full excruciating detail - hashmap 305 00:15:57,889 --> 00:15:59,678 306 00:15:59,678 --> 00:16:04,289 from strings to integer [inaudible] the 307 00:16:04,289 --> 00:16:05,679 constructor, 308 00:16:05,679 --> 00:16:08,549 and that's gonna create a hashmap from strings to integers for you. The first 309 00:16:08,549 --> 00:16:11,539 thing doesn't always have to be a string. It can be whatever you want, right? You can have a hashmap 310 00:16:11,539 --> 00:16:13,980 from 311 00:16:13,980 --> 00:16:17,580 integers to some other integer, where you wanna map from integers to integers 312 00:16:17,580 --> 00:16:21,550 if you wanna do that. Some people do. I don't know why. Sometimes you do. 313 00:16:21,549 --> 00:16:25,299 Now the other interesting thing is because a hashmap actually 314 00:16:25,299 --> 00:16:27,449 implements this map interface, 315 00:16:27,450 --> 00:16:29,680 sometimes the way you actually see this written 316 00:16:29,679 --> 00:16:34,819 is you'll see it written without the hash in front. Okay? 317 00:16:34,820 --> 00:16:38,879 Just for the declaration. Here you still have to have the hash, and here you 318 00:16:38,879 --> 00:16:40,549 still have to have the hash, 319 00:16:40,549 --> 00:16:43,848 but just in terms of the type, sometimes it will be written 320 00:16:43,849 --> 00:16:47,699 map string to strings is a hashmap of strings to strings. And you should look at that, 321 00:16:47,698 --> 00:16:52,238 and at first you get a little bit tweaked out because you're like, "But Mehran, you told me a map 322 00:16:52,239 --> 00:16:56,259 wasn't a class. You told me it was an interface." Yeah. 323 00:16:56,259 --> 00:16:58,539 And a hashmap implements that interface. 324 00:16:58,539 --> 00:17:02,909 So what I'm saying is I'm saying I wanna create a map, that map 325 00:17:02,909 --> 00:17:06,389 that I'm gonna call a dictionary, and what's the actual thing that implements 326 00:17:06,390 --> 00:17:07,619 the dictionary? 327 00:17:07,618 --> 00:17:10,019 It's a hashmap. Okay? 328 00:17:10,019 --> 00:17:13,579 Because the hashmap implements the map interface, it will have all the methods 329 00:17:13,579 --> 00:17:14,928 the map expects, 330 00:17:14,929 --> 00:17:18,000 so any time I use this guy, and I say, "Hey, it's a map," 331 00:17:18,000 --> 00:17:21,140 and I call any of the methods for a map, it's perfectly fine because a 332 00:17:21,140 --> 00:17:24,660 hashmap is guaranteed to have implemented those because it implements the interface. 333 00:17:24,660 --> 00:17:29,620 So just so you see this in code, sometimes you'll actually see the hash out here, 334 00:17:29,619 --> 00:17:30,929 sometimes you won't. 335 00:17:30,930 --> 00:17:32,440 Sometimes you feel like a nut, 336 00:17:32,440 --> 00:17:34,409 sometimes you don't. You should see it both ways. 337 00:17:34,409 --> 00:17:38,749 It makes sense both ways, but you can't drop the hash here because a 338 00:17:38,749 --> 00:17:43,710 map is an abstract idea. You can't say give me a new abstract idea. 339 00:17:43,710 --> 00:17:46,750 Okay? That's not gonna work. You can say give me a new hashmap. That's the concrete 340 00:17:46,750 --> 00:17:47,210 thing. 341 00:17:47,210 --> 00:17:50,380 And what am I gonna assign that concrete thing to? Well, this concrete thing 342 00:17:50,380 --> 00:17:52,760 implements the abstract idea, so it's perfectly fine 343 00:17:52,759 --> 00:17:57,160 to say, "Yeah, here's the concrete thing. Use it in the place of the abstract idea." 344 00:17:57,160 --> 00:17:59,420 Any questions about that? 345 00:17:59,420 --> 00:18:01,269 Kinda funky, but that's the way it is. 346 00:18:01,269 --> 00:18:03,389 So when we have these key value pairs, 347 00:18:03,390 --> 00:18:08,409 how do we actually add stuff to our hashmap and pull stuff out of our hashmap? 348 00:18:08,409 --> 00:18:11,470 So there's two methods. There's a method called put 349 00:18:11,470 --> 00:18:15,100 and a method called get. These are the two most commonly used methods of the 350 00:18:15,099 --> 00:18:17,158 hashmap. And the way these things work 351 00:18:17,159 --> 00:18:18,230 is put - let 352 00:18:18,230 --> 00:18:20,819 me write them more toward the middle of the board. 353 00:18:20,819 --> 00:18:25,389 Put strangely enough - I know this is gonna be difficult to see, but put actually 354 00:18:25,390 --> 00:18:27,650 puts something in your hashmap. So the 355 00:18:27,650 --> 00:18:32,519 way put works is you give it a key and a value. The types of those things 356 00:18:32,519 --> 00:18:35,729 have to be the types corresponding to whatever 357 00:18:35,729 --> 00:18:39,190 object you've created, so if I wanna have - for example, I wanna put something in 358 00:18:39,190 --> 00:18:43,460 my dictionary, I'd send the put message to the dictionary hashmap, 359 00:18:43,460 --> 00:18:47,548 and I'd say, "Hey, add this key value pair," and key and value type 360 00:18:47,548 --> 00:18:49,129 strings to match this thing. 361 00:18:49,130 --> 00:18:50,560 Or if I have some 362 00:18:50,559 --> 00:18:52,220 phone book, I could say put 363 00:18:52,220 --> 00:18:55,180 key value where value better be an integer 364 00:18:55,180 --> 00:18:57,560 and key better be a string. 365 00:18:57,559 --> 00:19:00,639 Besides putting things - it's kinda fun if you can put a lot of things in a hashmap, but 366 00:19:00,640 --> 00:19:03,310 it's not so much fun if you can't get them out. It's kinda like you had 367 00:19:03,309 --> 00:19:03,769 368 00:19:03,769 --> 00:19:06,119 your phone book was kinda like your cell phone that you put all these numbers in, 369 00:19:06,119 --> 00:19:09,529 and then you tried to get a number out, and it just said, "No, I'm not gonna give you any 370 00:19:09,529 --> 00:19:10,250 numbers." 371 00:19:10,250 --> 00:19:13,319 You would probably quickly stop putting numbers in. Some people wouldn't. I don't 372 00:19:13,319 --> 00:19:16,419 know why, but I've seen that happen. 373 00:19:16,420 --> 00:19:19,450 You're like, "You've seen people not be able to get numbers 374 00:19:19,450 --> 00:19:20,190 out of their cell phone?" Yeah, like 375 00:19:20,190 --> 00:19:23,180 their cell phone's busted and they just can't deal with that. 376 00:19:23,180 --> 00:19:26,039 But that's not important right now. What's important right now is you actually wanna be able 377 00:19:26,039 --> 00:19:27,879 to get values out. 378 00:19:27,880 --> 00:19:33,270 So out of our dictionary, you might say, "Hey, Mehran. I wanna get some particular word." 379 00:19:33,269 --> 00:19:37,569 I wanna get the definition for a word, really. This word is my key, so one way 380 00:19:37,569 --> 00:19:42,119 I could think of it is it's a word I'm looking up, but in the abstract notion, it's 381 00:19:42,119 --> 00:19:44,819 really a key. And so what this gives me back 382 00:19:44,819 --> 00:19:49,369 when I call get on the key is it goes over here and says, "Hey, dictionary. 383 00:19:49,369 --> 00:19:53,548 Do you have some value associated with the particular key?" 384 00:19:53,548 --> 00:19:58,349 So if this key exists in the hashmap, what it gives you back - so it will return to 385 00:19:58,349 --> 00:20:00,709 you the corresponding value. 386 00:20:00,710 --> 00:20:07,710 If that key doesn't exist in the hashmap, it gives you back null if key not found. Okay? 387 00:20:10,339 --> 00:20:13,939 So that's the way you can determine is this thing actually exist in my hashmap or not. If I try to 388 00:20:13,940 --> 00:20:17,860 do a get on it and it's not there, I'm gonna get back a null. 389 00:20:17,859 --> 00:20:18,609 Okay? So 390 00:20:18,609 --> 00:20:22,479 any questions about that? 391 00:20:22,480 --> 00:20:26,329 Uh huh? If the value's an integer, will it still give you back null? 392 00:20:26,329 --> 00:20:29,759 Yeah, because integer is a class, so you're gonna get back 393 00:20:29,759 --> 00:20:30,980 394 00:20:30,980 --> 00:20:34,960 object of type integer, and if you happen to 395 00:20:34,960 --> 00:20:38,769 look up something that's not there, null is a - it's the empty object. 396 00:20:38,769 --> 00:20:41,908 That's why you can't do int because there is no null int. Good 397 00:20:41,909 --> 00:20:43,840 398 00:20:43,839 --> 00:20:47,428 question. So let's create a little hashmap, add a couple values to it. 399 00:20:47,429 --> 00:20:52,490 So if let's say I had my phone book, I could say phone book 400 00:20:52,490 --> 00:20:53,538 dot 401 00:20:53,538 --> 00:20:55,200 put - always 402 00:20:55,200 --> 00:20:56,830 a dangerous thing 403 00:20:56,829 --> 00:21:00,298 to give out your phone number, but it's on the first handout. 404 00:21:00,298 --> 00:21:01,930 It's 7236059. 405 00:21:01,930 --> 00:21:03,840 Call. It's a good time. 406 00:21:03,839 --> 00:21:05,869 So that's just gonna put - 407 00:21:05,869 --> 00:21:07,169 this thing 408 00:21:07,170 --> 00:21:08,909 is an int, right? 409 00:21:08,909 --> 00:21:11,870 So in order for it to become integer, what's gonna happen is this is 410 00:21:11,869 --> 00:21:15,339 automatically gonna get boxed up for you as an integer, 411 00:21:15,339 --> 00:21:18,579 and then that's what's gonna get stored. So this autoboxing is what's happening 412 00:21:18,579 --> 00:21:21,519 to you, which makes it a little bit more seamless to use it with an int, 413 00:21:21,519 --> 00:21:24,869 but if you try to do this in an older version than Java 5.0, it'll actually 414 00:21:24,869 --> 00:21:27,829 give you an error because it didn't do the boxing for you. 415 00:21:27,829 --> 00:21:30,329 Notice there's no dash in there by the way because then my phone number would be 416 00:21:30,329 --> 00:21:33,769 some negative number because it'd be 723 minus 417 00:21:33,769 --> 00:21:34,859 6059, and 418 00:21:34,859 --> 00:21:36,189 that's a bad 419 00:21:36,190 --> 00:21:40,990 time. So then we could add someone else to the phone book. 420 00:21:40,990 --> 00:21:43,730 Put Jenny - 421 00:21:43,730 --> 00:21:47,069 anyone know Jenny's phone number? 422 00:21:47,069 --> 00:21:51,149 8675309. 8675309. 423 00:21:51,150 --> 00:21:55,600 It's amazing how much longevity that song - like I remember listening to that 424 00:21:55,599 --> 00:21:56,250 song 425 00:21:56,250 --> 00:21:57,549 when I was in high school. 426 00:21:57,549 --> 00:22:00,359 It's such a catchy tune. 427 00:22:00,359 --> 00:22:03,979 And if you don't - you have no idea what I'm talking about, 428 00:22:03,980 --> 00:22:05,110 look it up. 429 00:22:05,109 --> 00:22:06,319 430 00:22:06,319 --> 00:22:09,829 Just query 8675309. You'll find it on Wikipedia. 431 00:22:09,829 --> 00:22:13,119 It's not important. It's really totally irrelevant, but it's just fun. All 432 00:22:13,119 --> 00:22:16,898 right. So once I have these entries in the phone book, I could say something 433 00:22:16,898 --> 00:22:18,559 integer 434 00:22:18,559 --> 00:22:19,179 mnum, 435 00:22:19,180 --> 00:22:24,060 because that's what I wanna be Mehran's number, equals phone 436 00:22:24,059 --> 00:22:29,659 book dot get and give it Mehran. 437 00:22:29,660 --> 00:22:33,519 Okay? Important thing to remember is these are - the keys are all case 438 00:22:33,519 --> 00:22:37,789 sensitive, so if I put in a lower case M on Mehran here, it would return null because it 439 00:22:37,789 --> 00:22:39,329 wouldn't be able to it. So 440 00:22:39,329 --> 00:22:44,579 keys are always case sensitive in maps. Just an important thing to think about, okay? So 441 00:22:44,579 --> 00:22:45,899 with that said, 442 00:22:45,900 --> 00:22:49,100 we can actually get a little bit more concrete with the hashmap. I need a 443 00:22:49,099 --> 00:22:52,058 volunteer. 444 00:22:52,058 --> 00:22:55,910 Come on down. It's easy. You're up close. You're gonna 445 00:22:55,910 --> 00:22:58,360 be a hashmap. 446 00:22:58,359 --> 00:22:59,769 Here's your hashmap. 447 00:22:59,769 --> 00:23:02,210 I've just created you. 448 00:23:02,210 --> 00:23:04,659 You exist. Rock on. All right. 449 00:23:04,659 --> 00:23:06,049 So I hope 450 00:23:06,048 --> 00:23:09,859 you don't plan on taking notes any time soon because it might be difficult as the hashmap. 451 00:23:09,859 --> 00:23:13,719 So what we're gonna do is we're actually gonna enter - we're gonna call these commands on the 452 00:23:13,720 --> 00:23:17,049 hashmap so we can actually store these things in there, and then see what other commands - 453 00:23:17,049 --> 00:23:19,129 other things we can do. So 454 00:23:19,130 --> 00:23:21,900 normally the thing that gives bulk underneath the hood to a hashmap is 455 00:23:21,900 --> 00:23:22,860 our friend 456 00:23:22,859 --> 00:23:23,919 the Ding Dong. 457 00:23:23,920 --> 00:23:27,890 So what we're gonna have is certain things that we're gonna enter in our hashmap, which is basically a 458 00:23:27,890 --> 00:23:29,210 Ding Dong sandwich, 459 00:23:29,210 --> 00:23:32,100 and on one side we're gonna have the key, and on the other side, we're gonna have the 460 00:23:32,099 --> 00:23:32,759 value. 461 00:23:32,759 --> 00:23:35,660 So what I'm gonna do is I'm gonna write the keys in black 462 00:23:35,660 --> 00:23:38,700 and the values in red so we can keep track of them. That way we won't get them 463 00:23:38,700 --> 00:23:41,250 confused in our minds. So the first key I'm actually adding 464 00:23:41,250 --> 00:23:44,429 is I'm gonna add myself. 465 00:23:44,429 --> 00:23:45,460 Mehran - 466 00:23:45,460 --> 00:23:48,788 I guess I could've written this before, but I didn't, so I have my key. 467 00:23:48,788 --> 00:23:52,819 And that key has with it a value, and the value sticks with it, 468 00:23:52,819 --> 00:23:58,389 so 7236059 - I shouldn't have put the dash in there, but I accidentally put 469 00:23:58,390 --> 00:24:01,570 the dash in there, and I say put. And this 470 00:24:01,569 --> 00:24:03,808 goes into the hashmap. Okay? 471 00:24:03,808 --> 00:24:07,389 At this point, I have no notion of what's in my hashmap, or ordering, or whatever. I 472 00:24:07,390 --> 00:24:11,250 just created something. I tossed it in the hashmap. So then I say, "Well, Jenny, 473 00:24:11,250 --> 00:24:12,719 she was always 474 00:24:12,719 --> 00:24:16,920 a good time." 475 00:24:16,920 --> 00:24:20,480 You've gotta listen to the song, all right? If you have no context for the song, you're like, 476 00:24:20,480 --> 00:24:21,999 "That's not funny." 477 00:24:21,999 --> 00:24:23,230 478 00:24:23,230 --> 00:24:25,179 8675309 - we 479 00:24:25,179 --> 00:24:27,090 480 00:24:27,089 --> 00:24:31,028 put Jenny in the hashmap. 481 00:24:31,028 --> 00:24:33,839 Now that we have these things in the hashmap, there's an interesting thing that comes 482 00:24:33,839 --> 00:24:35,769 up, which is 483 00:24:35,769 --> 00:24:38,279 when I had an array list, 484 00:24:38,279 --> 00:24:41,730 I knew how many things were in my array list. I could refer to the first element. I could refer to 485 00:24:41,730 --> 00:24:43,210 the second element. 486 00:24:43,210 --> 00:24:47,329 What's the first element that's in my hashmap? You're 487 00:24:47,329 --> 00:24:49,669 like, "Well, you put in Mehran first," 488 00:24:49,670 --> 00:24:52,779 but it's not guaranteed to necessarily be the first element. 489 00:24:52,779 --> 00:24:57,288 A map has no intrinsic ordering. It's all just kinda mushed in there. 490 00:24:57,288 --> 00:25:00,379 So there are some things we might additionally wanna do on the hash map 491 00:25:00,380 --> 00:25:04,679 to give us some notion of what's in the hashmap and how big the hashmap is. 492 00:25:04,679 --> 00:25:07,700 So here's a couple more methods, and you could just bear with me. 493 00:25:07,700 --> 00:25:09,509 I swear it will be over soon, 494 00:25:09,509 --> 00:25:12,579 the pain and the agony. No, that's a good time. 495 00:25:12,579 --> 00:25:13,808 All right. So 496 00:25:13,808 --> 00:25:17,399 a couple other methods - I might have to ask you to just move over slightly - 497 00:25:17,400 --> 00:25:19,740 is we can remove a key. 498 00:25:19,740 --> 00:25:23,440 So if I ask to remove a key, what I pass it is the key, so I might say 499 00:25:23,440 --> 00:25:27,410 something like phone book 500 00:25:27,410 --> 00:25:29,700 dot remove and I give it some key. 501 00:25:29,700 --> 00:25:33,529 Now if that key exists in the phone book, it removes it from the hash map, and it 502 00:25:33,529 --> 00:25:35,329 is now gone. So if I call 503 00:25:35,329 --> 00:25:37,089 remove on 504 00:25:37,089 --> 00:25:38,589 Mehran - oh, hash map, I need Mehran. 505 00:25:38,589 --> 00:25:41,699 So you look somewhere inside the hashmap and you say, 506 00:25:41,700 --> 00:25:45,330 "Oh, here's Mehran. Remove. 507 00:25:45,329 --> 00:25:46,949 Phone number goes with it." 508 00:25:46,950 --> 00:25:50,269 That's just the way it is. The key and the value always stay together. 509 00:25:50,269 --> 00:25:54,950 Now besides removing something - and if Mehran didn't exist - so I could actually say 510 00:25:54,950 --> 00:25:56,509 remove Bob. 511 00:25:56,509 --> 00:25:57,609 You look in there. 512 00:25:57,609 --> 00:25:58,939 There's no Bob. 513 00:25:58,940 --> 00:26:02,240 You don't need to do anything with the hashmap. It's sort of this simple 514 00:26:02,240 --> 00:26:05,150 operation. There's no exception thrown, nothing like that. I asked to remove something. 515 00:26:05,150 --> 00:26:06,220 It's not in there. 516 00:26:06,220 --> 00:26:08,150 We just keep sticking with it. But 517 00:26:08,150 --> 00:26:14,509 what I can actually ask is, "Oh phone book, do you contain a key?" And so there's a method, 518 00:26:14,509 --> 00:26:15,690 contains key, 519 00:26:15,690 --> 00:26:18,769 that I give it a key and it returns to me a Boolean. 520 00:26:18,769 --> 00:26:20,819 So I can ask you, "Oh phone book, do 521 00:26:20,819 --> 00:26:23,798 you contains key Jenny?" True. 522 00:26:23,798 --> 00:26:25,619 True. Good times. Phone 523 00:26:25,619 --> 00:26:28,379 book, do you contains key Mehran? 524 00:26:28,380 --> 00:26:29,550 False. False 525 00:26:29,549 --> 00:26:32,349 because the key's been thrown out, right? We already removed that key. 526 00:26:32,349 --> 00:26:35,129 And last but not least, we can ask the phone book 527 00:26:35,130 --> 00:26:42,130 for its size. So this will return an int. Phone book, what's your size? One. Excellent. 528 00:26:42,160 --> 00:26:44,880 Thank you very much. And you're done, phone book. 529 00:26:44,880 --> 00:26:47,280 Thank you very much. Nice work. 530 00:26:47,279 --> 00:26:49,298 And for that, 531 00:26:49,298 --> 00:26:52,788 we'll just - you and everyone around you. It's an array list of 532 00:26:52,788 --> 00:26:56,669 Ding Dongs. [Inaudible]. All right. 533 00:26:56,669 --> 00:26:59,890 So that's the way you wanna think about it. It's this bag. You put stuff in 534 00:26:59,890 --> 00:27:03,330 the bag. You take stuff out of the bag. Whenever you put stuff in the bag, you 535 00:27:03,329 --> 00:27:08,369 put them in in pairs. When you take stuff out of the bag, it's always pairs. And 536 00:27:08,369 --> 00:27:12,069 all the work that you're doing is always in terms of your key, 537 00:27:12,069 --> 00:27:15,439 but the value is associated with it. So when you wanna remove stuff or check 538 00:27:15,440 --> 00:27:19,210 to see if it contains, you never look based on the value. You always look based 539 00:27:19,210 --> 00:27:23,180 on the key. That's just the way things are. Now 540 00:27:23,180 --> 00:27:26,730 this whole notion of maps is part of a bigger framework, so if we can get the 541 00:27:26,730 --> 00:27:30,529 computer for a second, I'll show you the bigger framework just real briefly. 542 00:27:30,529 --> 00:27:33,250 There's this notion called the collections hierarchy, 543 00:27:33,250 --> 00:27:36,909 which is - you're like, "Oh my god. It's big. It's huge." No. 544 00:27:36,909 --> 00:27:39,019 Most of the stuff you don't need to worry about. 545 00:27:39,019 --> 00:27:42,048 There's some of this stuff you've already seen. An array list you've seen. 546 00:27:42,048 --> 00:27:45,759 An array list is something called an abstract list, which is just the 547 00:27:45,759 --> 00:27:47,089 abstract concept of a list, 548 00:27:47,089 --> 00:27:50,990 and that's part - that actually implements an interface called list, 549 00:27:50,990 --> 00:27:54,269 which implements an interface called a collection. 550 00:27:54,269 --> 00:27:57,569 So array list, this thing you've been using this whole time, we didn't tell you 551 00:27:57,569 --> 00:28:01,428 until now is actually something that we could refer to as a collection in the 552 00:28:01,429 --> 00:28:04,100 same way that we could refer to a hash map as a map. 553 00:28:04,099 --> 00:28:06,740 Now there's a few other things in here that we're just not gonna deal with in this 554 00:28:06,740 --> 00:28:08,339 class, like a tree set. 555 00:28:08,339 --> 00:28:11,009 Or you might notice there's something over here called a hash set. 556 00:28:11,009 --> 00:28:14,180 Hash set and hashmap, not the same thing. 557 00:28:14,180 --> 00:28:17,060 So you should just know that. You don't need to know what a hash set is. You 558 00:28:17,059 --> 00:28:20,470 just need to know that it's not a hashmap because the words aren't the same. If 559 00:28:20,470 --> 00:28:23,400 they were the same, then it would be called a hashmap. But 560 00:28:23,400 --> 00:28:28,009 there's this notion of an abstract set that they are subclasses of that 561 00:28:28,009 --> 00:28:30,230 implement some abstract notion of a set, 562 00:28:30,230 --> 00:28:32,839 so there's this big kinda complicated hierarchy, 563 00:28:32,839 --> 00:28:35,699 but the important thing to remember is all of this stuff at the end of the day 564 00:28:35,700 --> 00:28:38,910 is a collection. So there might be some operations we might learn how to do 565 00:28:38,910 --> 00:28:39,800 on collections 566 00:28:39,799 --> 00:28:43,089 that applies to all of it because all of these things at the end of the day 567 00:28:43,089 --> 00:28:44,990 implement the collections interface. 568 00:28:44,990 --> 00:28:48,419 There's a same kind of notion for hashmaps. The hierarchy's just a little bit 569 00:28:48,419 --> 00:28:51,390 smaller so it's easier to see. 570 00:28:51,390 --> 00:28:55,309 A hashmap is this notion of an abstract map, which we talked about. It's kind of 571 00:28:55,308 --> 00:28:58,028 an abstract concept that has key value pairs, 572 00:28:58,028 --> 00:29:01,809 and what really defines that is an interface that's called a map. 573 00:29:01,809 --> 00:29:05,139 Now it turns out that besides hashmaps there's another kind of map 574 00:29:05,140 --> 00:29:06,730 called a tree map, 575 00:29:06,730 --> 00:29:08,929 and a tree map also allows you to 576 00:29:08,929 --> 00:29:11,109 put things into it and get things out of it. 577 00:29:11,108 --> 00:29:15,058 The underlying implementation is just a different way underneath the hood of 578 00:29:15,058 --> 00:29:17,149 actually implementing a map. 579 00:29:17,150 --> 00:29:20,169 And that's the whole critical concept here. The critical concept 580 00:29:20,169 --> 00:29:24,110 is the abstraction that the idea of a map interface gives you. 581 00:29:24,109 --> 00:29:27,169 When you think about something that implements the map interface, all you need 582 00:29:27,170 --> 00:29:28,129 to worry about 583 00:29:28,128 --> 00:29:30,808 is what are the methods that are involved in a map. 584 00:29:30,808 --> 00:29:34,548 Underneath the hood, hashmap may have some different methods than tree map for 585 00:29:34,548 --> 00:29:36,098 some specialized kind of things, 586 00:29:36,098 --> 00:29:39,158 but we don't care. As long as we just treat 587 00:29:39,159 --> 00:29:40,750 everything as a map, 588 00:29:40,750 --> 00:29:43,930 someone someday comes along and there's the hashmap, the tree map, and then they 589 00:29:43,930 --> 00:29:46,380 create the super cool funky map. 590 00:29:46,380 --> 00:29:49,470 And you're like, "Wow, the super cool funky map is just way more efficient than all 591 00:29:49,470 --> 00:29:51,088 the other maps that existed," because 592 00:29:51,088 --> 00:29:54,288 someone had this great idea about how to implement maps like in 593 00:29:54,288 --> 00:29:55,390 2027. 594 00:29:55,390 --> 00:29:57,840 Well, if you go back through your code and everything's written in terms of a map 595 00:29:57,839 --> 00:29:58,589 interface, 596 00:29:58,589 --> 00:30:00,669 and super funky cool map - 597 00:30:00,670 --> 00:30:03,950 was that super funky efficient map? Gotta get the name right - 598 00:30:03,950 --> 00:30:07,650 actually implements the map interface, none of your code changes, except in one 599 00:30:07,650 --> 00:30:09,960 line when you create your map. 600 00:30:09,960 --> 00:30:14,298 Rather than saying map string string hashmap, you say map string string 601 00:30:14,298 --> 00:30:15,859 super funky cool map. 602 00:30:15,859 --> 00:30:19,129 And everywhere else in your code when you refer to dictionary, you're just 603 00:30:19,130 --> 00:30:22,249 using the methods of the map interface and no other code changes. 604 00:30:22,249 --> 00:30:25,528 So that's the power of thinking of the notion of object oriented programming and 605 00:30:25,528 --> 00:30:28,679 object oriented design in these abstraction hierarchies 606 00:30:28,679 --> 00:30:29,839 is the fact that 607 00:30:29,838 --> 00:30:32,958 as new implementations come along - and they really do, right? It's not like computer 608 00:30:32,959 --> 00:30:36,009 science is a static thing. Ten years from now, there will be other implementations 609 00:30:36,009 --> 00:30:36,839 of these things 610 00:30:36,839 --> 00:30:39,470 which will be more efficient and better than what exists now. 611 00:30:39,470 --> 00:30:43,009 But if we relied on always thinking about hashmap, 612 00:30:43,009 --> 00:30:47,569 then our code would be much more rigid than if we just think about this abstract concept 613 00:30:47,569 --> 00:30:51,240 of map. Any questions about that? All right. 614 00:30:51,240 --> 00:30:54,660 So think abstractly. That's kinda the bottom line 615 00:30:54,660 --> 00:30:56,380 from that whole little 616 00:30:56,380 --> 00:30:57,160 diatribe. 617 00:30:57,160 --> 00:30:58,529 So 618 00:30:58,529 --> 00:31:01,428 the one thing that we wanna think about is 619 00:31:01,429 --> 00:31:05,470 when we have these things like maps and array lists, and I told you an array list is part of the 620 00:31:05,470 --> 00:31:08,690 collection, what do I actually get with these things? What does a 621 00:31:08,690 --> 00:31:11,960 collection buy me other than the fact that I get to draw this complicated picture and be 622 00:31:11,960 --> 00:31:15,240 like, "Oh, look. This is a collection framework?" 623 00:31:15,240 --> 00:31:19,400 What it gives you that's interesting is a notion of what we call an iterator. 624 00:31:19,400 --> 00:31:20,830 625 00:31:20,829 --> 00:31:22,458 What is an iterator? An 626 00:31:22,459 --> 00:31:24,788 iterator is just an idea basically. 627 00:31:24,788 --> 00:31:31,788 It's a way to list through a set of values. 628 00:31:32,049 --> 00:31:33,158 What does that mean? 629 00:31:33,159 --> 00:31:36,600 What that means is if I have some array list, and that array list contains 630 00:31:36,599 --> 00:31:40,178 like ten different items in it, one way I could think about that array list is 631 00:31:40,179 --> 00:31:44,159 having some for loop that counts from zero up to nine, and I get each one of the individual 632 00:31:44,159 --> 00:31:45,190 values. 633 00:31:45,190 --> 00:31:47,880 Another way I can think about that array list because an array list is really a 634 00:31:47,880 --> 00:31:49,020 collection 635 00:31:49,019 --> 00:31:53,249 is if all collections allow me to have an iterator, this iterator will 636 00:31:53,249 --> 00:31:56,230 allow me to very easily - and I'll show you the syntax - 637 00:31:56,230 --> 00:31:58,329 have a sequential 638 00:31:58,329 --> 00:32:01,220 process of going through all the values in my array list. 639 00:32:01,220 --> 00:32:03,778 And for an array list, it's not as exciting as some other things because you're 640 00:32:03,778 --> 00:32:06,690 like, "But I already have a way of going sequentially through my array list." So let's 641 00:32:06,690 --> 00:32:09,220 just compare and contrast them. 642 00:32:09,220 --> 00:32:13,038 The idea when you think about having a list of values 643 00:32:13,038 --> 00:32:16,599 and having an iterator is - first of all, let's create an array list, and I'll show you 644 00:32:16,599 --> 00:32:20,888 how to get an iterator from it. So let's say we have an array list of strings. 645 00:32:20,888 --> 00:32:26,250 And we'll call this names because it's gonna just store a bunch of names for us, and so we say new array list. It's an 646 00:32:26,250 --> 00:32:30,368 array list of string. We're 647 00:32:30,368 --> 00:32:32,939 calling a constructor so we have the prints. 648 00:32:32,940 --> 00:32:37,610 Then what I say is, "Hey, array list. I want some way to be able to go through all your 649 00:32:37,609 --> 00:32:41,189 elements in a very easy way in an abstract way 650 00:32:41,190 --> 00:32:44,390 because I don't wanna have this grungy syntax of asking you for your size and 651 00:32:44,390 --> 00:32:47,450 having a for loop." That's something that's very specific to an array list. 652 00:32:47,450 --> 00:32:50,190 What I really wanna do is kind of have this generalization to say, 653 00:32:50,190 --> 00:32:52,650 "Hey, you contain some set of values, 654 00:32:52,650 --> 00:32:57,100 and I just wanna go through each one of your values one by one." 655 00:32:57,099 --> 00:33:02,349 So the way I do that is I create an iterator. An iterator has a templatized 656 00:33:02,349 --> 00:33:02,939 type 657 00:33:02,940 --> 00:33:05,429 because if I have an array list of strings 658 00:33:05,429 --> 00:33:07,830 and I wanna go through all the values one at a time, 659 00:33:07,829 --> 00:33:13,249 the values are all strings, which means I need to have an iterator that iterates over strings. 660 00:33:13,249 --> 00:33:17,610 So the types are generally the same. If I have an array list that I'm gonna iterate over, its 661 00:33:17,609 --> 00:33:20,449 iterator is always gonna be the same type as the type of the array list. 662 00:33:20,450 --> 00:33:24,920 And I'll just call this it. And it, which is - oftentimes you'll see iterators often called 663 00:33:24,920 --> 00:33:28,210 I or it just for shorthand - 664 00:33:28,210 --> 00:33:34,210 is named dot, and now it's time for you to see a new method of the array list, 665 00:33:34,210 --> 00:33:39,670 iterator. So what this method does is it says, "Hey, array list. 666 00:33:39,670 --> 00:33:43,450 Give me an iterator over your values." And what you get is something whose type is 667 00:33:43,450 --> 00:33:47,000 iterator of strings. Now how do you actually use that? 668 00:33:47,000 --> 00:33:51,509 Here's how you use it. 669 00:33:51,509 --> 00:33:52,970 So what you're gonna do 670 00:33:52,970 --> 00:33:55,390 is you're gonna have a while loop. 671 00:33:55,390 --> 00:33:58,930 And the way an iterator works is you can basically ask the iterator two questions. You 672 00:33:58,930 --> 00:34:02,630 can say, "Hey, iterator. Are there any values left in you? 673 00:34:02,630 --> 00:34:06,640 And if there are, give me the next values." So the way you ask to see if there's any 674 00:34:06,640 --> 00:34:11,539 values left in the iterator is you say, "Hey, it. Do you have a next value?" so 675 00:34:11,539 --> 00:34:17,829 has next. That returns to you a Boolean if it has a next value or not. 676 00:34:17,829 --> 00:34:21,059 And then the way you get the next value interestingly enough is you just 677 00:34:21,059 --> 00:34:27,499 say, "Hey, iterator. Give me the next value," which returns to you 678 00:34:27,498 --> 00:34:29,148 whatever your type 679 00:34:29,148 --> 00:34:30,460 your iterator's 680 00:34:30,460 --> 00:34:32,019 templatized type 681 00:34:32,019 --> 00:34:35,108 is. So if you have an array list of strings, and you have an iterator over 682 00:34:35,108 --> 00:34:38,318 strings, when you call it dot next, you will get a single string. 683 00:34:38,318 --> 00:34:42,139 So if our array list over here - let's just say names - 684 00:34:42,139 --> 00:34:43,829 contain the names 685 00:34:43,829 --> 00:34:45,009 Bob, 686 00:34:45,009 --> 00:34:47,148 Alice, and 687 00:34:47,148 --> 00:34:49,248 a C name, Cal. 688 00:34:49,248 --> 00:34:52,028 Not very popular, maybe it'll come into vogue 689 00:34:52,028 --> 00:34:53,639 someday. 690 00:34:53,639 --> 00:34:55,440 Name your child - I was thinking, 691 00:34:55,440 --> 00:35:00,588 yeah, I should name my child Cal Sahami. That would be so wrong on so many levels, 692 00:35:00,588 --> 00:35:04,018 but that's not important right now, besides the fact that my wife would probably beat me to 693 00:35:04,018 --> 00:35:06,139 death. 694 00:35:06,139 --> 00:35:09,868 But what this really gives you - names is this array list 695 00:35:09,869 --> 00:35:13,689 is when I create the iterator - you can think of the iterator sort of sitting at the 696 00:35:13,688 --> 00:35:17,469 first value. And I say, "Hey, do you have a next value?" and it says, "Yeah, I do." It doesn't give 697 00:35:17,469 --> 00:35:19,858 it to you. I just says, "Yeah, I have a next value." 698 00:35:19,858 --> 00:35:25,078 "Oh, give me your next value." So it gives you back Bob as a string. Bob still is 699 00:35:25,079 --> 00:35:27,048 part of your array list. It doesn't go away, 700 00:35:27,048 --> 00:35:30,469 but now your iterator sort of automatically moves itself to the next element. 701 00:35:30,469 --> 00:35:32,449 So when you say, "Do you have a next element?" "Yeah." 702 00:35:32,449 --> 00:35:35,899 "Give it to me." You get Alice. And it moves itself down, and then yes for the 703 00:35:35,900 --> 00:35:38,949 third value, and you get Cal. And 704 00:35:38,949 --> 00:35:42,628 after you get Cal, you say, "Hey, iterator. Do you have a next value?" So after it's 705 00:35:42,628 --> 00:35:47,489 given you Cal, it's sort of gone off the end, and it says, "I don't have a next value." 706 00:35:47,489 --> 00:35:50,249 And that's why you always wanna check if it has a next value before you ask for it because if you ask 707 00:35:50,248 --> 00:35:55,598 for a next value when it does a next value, that's bad times. 708 00:35:55,599 --> 00:35:57,588 And if you really wanna know what that does, 709 00:35:57,588 --> 00:35:59,228 just try it in your code, and 710 00:35:59,228 --> 00:36:00,629 you can find out. 711 00:36:00,630 --> 00:36:04,130 It's always good to try to try some of the error conditions yourself, but 712 00:36:04,130 --> 00:36:07,570 this is the simple idea. Notice there's no notion of asking the array list for 713 00:36:07,570 --> 00:36:09,028 its size, 714 00:36:09,028 --> 00:36:13,108 or anything like that, or needing to know that the array list is actually ordered. 715 00:36:13,108 --> 00:36:15,838 And the important thing there is because there are sometimes - there are some lists 716 00:36:15,838 --> 00:36:19,078 like array lists which are ordered. We know that Bob is element zero, 717 00:36:19,079 --> 00:36:20,329 Alice is element one, 718 00:36:20,329 --> 00:36:21,880 Cal is element two. 719 00:36:21,880 --> 00:36:25,170 But we just talked about our friend the hashmap, and now you're probably thinking, "Yeah, 720 00:36:25,170 --> 00:36:27,900 Mehran. Why were you talking about all this array list stuff when you're talking about the 721 00:36:27,900 --> 00:36:28,608 hashmap?" 722 00:36:28,608 --> 00:36:30,639 Because the hashmap didn't have any ordering 723 00:36:30,639 --> 00:36:32,038 over its keys. 724 00:36:32,039 --> 00:36:35,839 And so one of the things we can do is we can ask the hashmap, 725 00:36:35,838 --> 00:36:39,469 "Hey, what I want is an iterator over your keys." 726 00:36:39,469 --> 00:36:44,568 So let's see an example of that. So 727 00:36:44,568 --> 00:36:46,940 in terms of thinking about the hashmap 728 00:36:46,940 --> 00:36:49,920 - and we could've done the same thing with a for loop in the case of an 729 00:36:49,920 --> 00:36:51,838 array list. It wouldn't have been very exciting, 730 00:36:51,838 --> 00:36:55,599 but I would completely trust that you could probably do that, or maybe you 731 00:36:55,599 --> 00:36:59,359 already did for your hangman assignment. Okay? 732 00:36:59,358 --> 00:37:02,139 When we think about a hashmap, a 733 00:37:02,139 --> 00:37:06,438 hashmap by itself doesn't - 734 00:37:06,438 --> 00:37:10,098 is not a collection, so it doesn't provide you with an iterator directly. 735 00:37:10,099 --> 00:37:13,349 And the reason why it doesn't provide you with an iterator directly is because 736 00:37:13,349 --> 00:37:16,180 if you were to say, "Hey, hashmap. Give me an iterator," it would say, 737 00:37:16,179 --> 00:37:19,578 "But I store key value pairs. I can't give you an iterator over key 738 00:37:19,579 --> 00:37:23,568 value pairs because an iterator is only defined by one type. 739 00:37:23,568 --> 00:37:27,659 But what I can give you is I can give you a set of my keys, 740 00:37:27,659 --> 00:37:31,670 and that set of keys is a collection, so you can ask it for an iterator." So 741 00:37:31,670 --> 00:37:32,849 the way that works 742 00:37:32,849 --> 00:37:38,509 is if I have some hashmap - let's say I have my phone book hashmap, 743 00:37:38,509 --> 00:37:41,509 I can say, "Oh phone book 744 00:37:41,509 --> 00:37:42,150 dot" - 745 00:37:42,150 --> 00:37:44,709 and the method is called keyset. 746 00:37:44,708 --> 00:37:49,419 Lower case K - it's sometimes hard to draw a lower case K, upper case S, 747 00:37:49,420 --> 00:37:52,409 so what that gives you back is a set of 748 00:37:52,409 --> 00:37:56,309 just the keys, so just the strings of the names in the phone book. And then 749 00:37:56,309 --> 00:37:59,929 you can ask that keyset dot - so this would all be on one line. 750 00:37:59,929 --> 00:38:01,019 "Hey, keyset. 751 00:38:01,019 --> 00:38:02,509 You're a collection. Give me 752 00:38:02,509 --> 00:38:03,639 an iterator." 753 00:38:03,639 --> 00:38:07,599 So you would say iterator. 754 00:38:07,599 --> 00:38:10,710 And what you're gonna get back is you're gonna fum phone book. 755 00:38:10,710 --> 00:38:14,050 "Phone book," you're gonna say, "What's your keyset?" and you get a set of keys. What's the type for 756 00:38:14,050 --> 00:38:16,999 the keys of phone book? 757 00:38:16,998 --> 00:38:19,228 Not a rhetorical question. 758 00:38:19,228 --> 00:38:19,818 Strings. 759 00:38:19,818 --> 00:38:22,420 So if I say, "You're a string set. 760 00:38:22,420 --> 00:38:24,030 String set, give me an iterator," 761 00:38:24,030 --> 00:38:27,940 what you get back is an iterator over string, so I can assign that over here 762 00:38:27,940 --> 00:38:30,039 by having iterator 763 00:38:30,039 --> 00:38:32,630 of strings, 764 00:38:32,630 --> 00:38:36,389 and I'll just call this I equals phone book dot keyset. I need to have 765 00:38:36,389 --> 00:38:39,669 these parens in here because I'm actually making a method call. This looks a little bit funky, but 766 00:38:39,668 --> 00:38:41,118 the way to think about it is 767 00:38:41,119 --> 00:38:44,250 phone book dot keyset is returning to you a 768 00:38:44,250 --> 00:38:45,699 object which is a set of keys, 769 00:38:45,699 --> 00:38:49,219 and then to that object, you're sending it the iterator message which is 770 00:38:49,219 --> 00:38:51,868 giving you back this iterator of strings. Okay? 771 00:38:51,869 --> 00:38:54,108 So once I get back 772 00:38:54,108 --> 00:38:55,348 this keyset 773 00:38:55,349 --> 00:38:58,048 from the hashmap and get its iterator, 774 00:38:58,048 --> 00:39:00,809 this exact same code over here 775 00:39:00,809 --> 00:39:02,949 works on that same iterator. 776 00:39:02,949 --> 00:39:06,849 So I could use this code on an iterator that was generated from an array list, 777 00:39:06,849 --> 00:39:10,869 or I could share exactly that same code from an iterator that comes from the 778 00:39:10,869 --> 00:39:12,509 keys of my phone book. 779 00:39:12,509 --> 00:39:15,780 So if I had my phone book that had Mehran and Jenny in it, 780 00:39:15,780 --> 00:39:17,749 what I would get is an iterator 781 00:39:17,748 --> 00:39:20,288 that's going to iterate over Mehran 782 00:39:20,289 --> 00:39:22,930 and Jenny. 783 00:39:22,929 --> 00:39:26,848 It has no notion of the actual values that are associated with those keys because 784 00:39:26,849 --> 00:39:31,469 the iterator is just over the set of keys, not the corresponding values. 785 00:39:31,469 --> 00:39:32,989 Any questions about that? 786 00:39:32,989 --> 00:39:35,728 The other thing that's funky, just to point one thing out, 787 00:39:35,728 --> 00:39:38,000 is in an array list, an array list is ordered, 788 00:39:38,000 --> 00:39:41,079 so I'm guaranteed to always get my set of 789 00:39:41,079 --> 00:39:46,048 keys from the iterator in the same order as they're stored in the array list. So 790 00:39:46,048 --> 00:39:49,559 I'll always get as the first key whatever's at index zero. The second key 791 00:39:49,559 --> 00:39:50,269 will be 792 00:39:50,269 --> 00:39:51,478 whatever's at index one, 793 00:39:51,478 --> 00:39:52,879 and so forth. 794 00:39:52,880 --> 00:39:53,838 Hashmap 795 00:39:53,838 --> 00:39:57,798 has no ordering. Even though I entered Mehran first and then I entered 796 00:39:57,798 --> 00:39:58,800 Jenny second, 797 00:39:58,800 --> 00:40:02,289 there is no actually no reason why Jenny couldn't show up as the first value. 798 00:40:02,289 --> 00:40:05,739 The only thing I'm guaranteed is that every value will show up once, 799 00:40:05,739 --> 00:40:07,969 but I get no guarantee over the ordering. 800 00:40:07,969 --> 00:40:09,739 So 801 00:40:09,739 --> 00:40:11,909 no order, 802 00:40:11,909 --> 00:40:14,348 and this one you always get order, 803 00:40:14,349 --> 00:40:17,229 just something to keep in mind. 804 00:40:17,228 --> 00:40:18,659 Was there a question? 805 00:40:18,659 --> 00:40:20,078 Or was that the question? 806 00:40:20,079 --> 00:40:24,559 Yeah, just for thinking of it, one step ahead of me. 807 00:40:24,559 --> 00:40:26,099 So 808 00:40:26,099 --> 00:40:29,469 one other piece of syntax that's a little bit interesting to see that you 809 00:40:29,469 --> 00:40:30,688 should also see - 810 00:40:30,688 --> 00:40:34,128 so any questions about the notion of a keyset or an iterator? 811 00:40:34,128 --> 00:40:36,679 If you're feeling okay with iterators, nod your head. 812 00:40:36,679 --> 00:40:39,858 If you're not feeling okay with iterators, shake your head. All 813 00:40:39,858 --> 00:40:44,429 right, question back there? 814 00:40:44,429 --> 00:40:48,918 Does keyset return an array or an array list? What does keyset return? It 815 00:40:48,918 --> 00:40:50,808 actually returns a set, 816 00:40:50,809 --> 00:40:53,829 and as far as you need to worry about it for this class, you don't need to worry about a 817 00:40:53,829 --> 00:40:58,159 set, so the only time you'll actually use the keyset syntax is with an iterator 818 00:40:58,159 --> 00:41:02,169 to get its iterator. But it actually is returning an object that is a set, or it 819 00:41:02,168 --> 00:41:06,998 actually implements the set interface. Okay? All righty. 820 00:41:06,998 --> 00:41:09,708 So one last thing to see, 821 00:41:09,708 --> 00:41:13,649 and then I'll show you all of this kind of stuck together in some code. 822 00:41:13,650 --> 00:41:17,460 Java actually gives you - because iterators have become such a common thing 823 00:41:17,460 --> 00:41:18,849 to do, 824 00:41:18,849 --> 00:41:22,179 like people were writing this code and left right - they were writing this code until the cows come 825 00:41:22,179 --> 00:41:25,659 home. And they were like, "Ah, but it's such a pain. I gotta create the iterator, and then I 826 00:41:25,659 --> 00:41:27,250 go through all the values, 827 00:41:27,250 --> 00:41:30,380 and that's such a common thing to do that I wanna go through the values, 828 00:41:30,380 --> 00:41:33,730 wouldn't it be nice if there were some simpler way of doing this?" And 829 00:41:33,730 --> 00:41:36,480 enough people sort of yelled and screamed this over the years that the people 830 00:41:36,480 --> 00:41:41,079 who created Java 5.0 said, "Okay. We're actually going to give you a version of the 831 00:41:41,079 --> 00:41:42,058 for loop 832 00:41:42,059 --> 00:41:45,149 which is totally different than what you've seen so far." So now you're old 833 00:41:45,148 --> 00:41:48,159 enough to see the new improved for loop. 834 00:41:48,159 --> 00:41:51,288 And the way this new, improved for loop looks like 835 00:41:51,289 --> 00:41:53,259 is you say for, 836 00:41:53,259 --> 00:41:55,389 you specify the type 837 00:41:55,389 --> 00:41:59,788 of the thing that you're going to get as every element of the loop - 838 00:41:59,789 --> 00:42:03,949 so in this case you would say something like string 839 00:42:03,949 --> 00:42:05,108 name. 840 00:42:05,108 --> 00:42:08,938 Then you give a colon, and then you specify over here 841 00:42:08,938 --> 00:42:12,688 the collection over which you would like an iterator. 842 00:42:12,688 --> 00:42:13,998 What does that mean? 843 00:42:13,998 --> 00:42:17,698 That means the collection that you would like an iterator over is whatever 844 00:42:17,699 --> 00:42:22,289 thing you are calling the iterator method on. You do not actually 845 00:42:22,289 --> 00:42:25,669 call the iterator method. You just specify the thing that you would've called the 846 00:42:25,668 --> 00:42:29,228 iterator method on. So over here what we would actually have is phone book 847 00:42:29,228 --> 00:42:30,688 dot keyset. 848 00:42:30,688 --> 00:42:33,219 Phone book 849 00:42:33,219 --> 00:42:36,938 dot - this would all be on the same line - keyset. 850 00:42:36,938 --> 00:42:37,958 Okay? 851 00:42:37,958 --> 00:42:40,168 And then 852 00:42:40,168 --> 00:42:41,489 inside this loop, 853 00:42:41,489 --> 00:42:43,849 what you can do is this 854 00:42:43,849 --> 00:42:45,559 word, this name 855 00:42:45,559 --> 00:42:48,340 of this variable is a declaration of a variable 856 00:42:48,340 --> 00:42:52,880 that sequentially will go through all of the elements of this collection over 857 00:42:52,880 --> 00:42:55,789 here. So this collection could be the keyset from a phone book. 858 00:42:55,789 --> 00:42:59,269 It could actually be an array list because over here we were asking the array list 859 00:42:59,269 --> 00:43:00,389 directly for an iterator. 860 00:43:00,389 --> 00:43:04,199 So we could say for every string and name colon names, 861 00:43:04,199 --> 00:43:06,009 and that means give me the name one at 862 00:43:06,009 --> 00:43:10,059 a time out of names, and inside here we could do something like 863 00:43:10,059 --> 00:43:12,719 print lin name. 864 00:43:12,719 --> 00:43:14,469 So name is just a variable. 865 00:43:14,469 --> 00:43:19,200 The way you can think of name is name is getting the value one at a time of 866 00:43:19,199 --> 00:43:22,458 an iterator over whatever you specified here. 867 00:43:22,458 --> 00:43:26,128 But the funky thing about this is you never created the iterator. You never said, 868 00:43:26,128 --> 00:43:28,139 "Hey, I need an iterator." 869 00:43:28,139 --> 00:43:29,670 You just said, "Yeah. 870 00:43:29,670 --> 00:43:32,170 This thing? You can get an iterator from that. 871 00:43:32,170 --> 00:43:35,849 And I wanna get things one at a time from this iterator, and the name 872 00:43:35,849 --> 00:43:38,910 I'm gonna specify for that thing getting one at a time is this 873 00:43:38,909 --> 00:43:41,748 variable over here, and it's gonna have this type." 874 00:43:41,748 --> 00:43:45,988 So this syntax, Java automatically what it will do is construct for you 875 00:43:45,989 --> 00:43:48,659 basically underneath the hood - you never need to worry about the syntax stuff for 876 00:43:48,659 --> 00:43:50,939 that - it will create an iterator for you. 877 00:43:50,938 --> 00:43:54,548 It will automatically check to see if the iterator has a next value. If it does, it 878 00:43:54,548 --> 00:43:58,579 will assign the next value to name, and then execute the body of your loop. 879 00:43:58,579 --> 00:44:01,570 And after it executes the body of your loop, it comes back up here, 880 00:44:01,570 --> 00:44:05,190 checks to see if iterator has a next value. If it doesn't, then it immediately leave 881 00:44:05,190 --> 00:44:05,858 the loop. 882 00:44:05,858 --> 00:44:09,710 If it does, it gets the next value, sticks it into this variable - so it clobbers the 883 00:44:09,710 --> 00:44:10,418 old value, 884 00:44:10,418 --> 00:44:13,088 but it sticks it into this variable, and then you can do whatever you want. So 885 00:44:13,088 --> 00:44:17,358 this will actually write out the whole list of keys from the phone book. 886 00:44:17,358 --> 00:44:21,898 And this only exists in Java 5.0 or later. And sometimes people 887 00:44:21,898 --> 00:44:25,248 refer to this as the for each construct, 888 00:44:25,248 --> 00:44:28,198 but the name's not as important as just understanding the syntax. And if you try 889 00:44:28,199 --> 00:44:31,969 to do it with something from Java in an earlier version than 5.0, it just 890 00:44:31,969 --> 00:44:33,949 doesn't work. 891 00:44:33,949 --> 00:44:35,838 So any questions about that? 892 00:44:35,838 --> 00:44:40,318 So let me show you some code that kinda puts this all together, 893 00:44:40,318 --> 00:44:41,739 a little code. 894 00:44:41,739 --> 00:44:44,759 So here's a little hashmap example. What I'm gonna do, and I put in a 895 00:44:44,760 --> 00:44:47,600 little bit of print lins here, just so you could see - like 896 00:44:47,599 --> 00:44:49,548 remember we talked about our little 897 00:44:49,548 --> 00:44:51,119 lecture on debugging? 898 00:44:51,119 --> 00:44:54,209 Where we said, "Hey, you know what? Before you call a function, just add a print 899 00:44:54,208 --> 00:44:55,969 lin. You know you got to that function. 900 00:44:55,969 --> 00:44:58,839 That's all you need to do." Then you know before you're gonna call this function, 901 00:44:58,840 --> 00:45:02,110 I'm about to call that function. It's the simplest form of debugging if you didn't 902 00:45:02,110 --> 00:45:05,930 have the full power of Eclipse 903 00:45:05,929 --> 00:45:07,108 available to you. 904 00:45:07,108 --> 00:45:10,380 So what we're gonna do is we're gonna read in phone numbers by reading in a list of phone 905 00:45:10,380 --> 00:45:11,000 numbers. 906 00:45:11,000 --> 00:45:13,400 What are we gonna read them into because we're not passing a parameter 907 00:45:13,400 --> 00:45:14,119 here? 908 00:45:14,119 --> 00:45:18,849 We must have some instance variable. So down here I have an instance variable 909 00:45:18,849 --> 00:45:20,000 that is - oh, and I forgot private in 910 00:45:20,000 --> 00:45:23,048 front of it. My bad. 911 00:45:23,048 --> 00:45:25,298 So it's a private instance variable. 912 00:45:25,298 --> 00:45:28,599 It's a map from strings to integers because this is our friend the phone book that we 913 00:45:28,599 --> 00:45:31,190 just talked about, and I'm gonna create 914 00:45:31,190 --> 00:45:33,999 for this map - notice I'm just saying map here. 915 00:45:33,998 --> 00:45:39,018 The actual implementation I'm gonna use is a hashmap from strings to integers. 916 00:45:39,018 --> 00:45:41,089 So it's the syntax you saw before, 917 00:45:41,090 --> 00:45:42,509 before I erased it. 918 00:45:42,509 --> 00:45:43,228 And then 919 00:45:43,228 --> 00:45:46,139 what we're gonna do is to read in the phone numbers is we're just gonna ask the 920 00:45:46,139 --> 00:45:46,699 user, 921 00:45:46,699 --> 00:45:50,239 "Give me a name." So we're gonna have a while true loop. Give me a name. 922 00:45:50,239 --> 00:45:54,688 We read in that name. If the name is equal to empty string, then I know the user's 923 00:45:54,688 --> 00:45:58,628 done entering names. And if they gave me a valid name, then I say, "Hey, give me a 924 00:45:58,628 --> 00:46:02,568 phone number as an integer." So I get the phone number's integer, and then I will add 925 00:46:02,568 --> 00:46:07,259 that name number pair to the phone book. Notice because this is an int, 926 00:46:07,259 --> 00:46:10,998 there's boxing going on here. This number's automatically getting boxed up 927 00:46:10,998 --> 00:46:15,088 for you to go from being an int to an integer so I can stick in a string and an integer 928 00:46:15,088 --> 00:46:16,429 into the phone book. 929 00:46:16,429 --> 00:46:19,460 Again, that only works in Java 5.0 or later, but that's what you should be 930 00:46:19,460 --> 00:46:23,318 using. So after I read in all these numbers from the user, I'm gonna allow you to look up 931 00:46:23,318 --> 00:46:25,929 numbers. And the way I'm gonna look up numbers is I'm gonna say - 932 00:46:25,929 --> 00:46:29,239 I'm gonna keep looking up numbers until you tell me you're done. So I'm gonna ask for a 933 00:46:29,239 --> 00:46:33,068 name to look up. If you're a done, you'll give me a name that equals the empty string 934 00:46:33,068 --> 00:46:35,079 to indicate that you're done looking for names. 935 00:46:35,079 --> 00:46:41,180 Otherwise, what I'll do is I'm gonna ask the phone book to get that name. 936 00:46:41,179 --> 00:46:43,618 What it's gonna give me is it's gonna go look up the name. It's gonna go look 937 00:46:43,619 --> 00:46:47,789 up Jenny in the phone book and say, "Hey, Jenny. Are you there?" And if Jenny's there, 938 00:46:47,789 --> 00:46:53,299 what I get back is the value associated with Jenny, 8675309, 939 00:46:53,298 --> 00:46:54,329 as this integer. 940 00:46:54,329 --> 00:46:57,339 I don't get back an int. I get back an integer. 941 00:46:57,338 --> 00:47:00,748 And this is critical. I shouldn't declare this as an int here, and the reason I 942 00:47:00,748 --> 00:47:03,348 shouldn't declare this as an int is because there's a chance that number could come 943 00:47:03,349 --> 00:47:04,660 back null. 944 00:47:04,659 --> 00:47:06,348 So if I say, "Hey, phone book. 945 00:47:06,349 --> 00:47:10,650 Give me the number for Bob," and it's like there's no Bob in the phone book, so here's null. 946 00:47:10,650 --> 00:47:15,509 I can't treat that null like an integer. I can't box and unbox the null, so 947 00:47:15,509 --> 00:47:19,028 I need to specifically check for that, and then I'll say that name is not in the phone book. 948 00:47:19,028 --> 00:47:22,108 Otherwise, what I'll do is I'll print it out. When I print it out, then it does the 949 00:47:22,108 --> 00:47:24,358 unboxing for me and prints out the integer. 950 00:47:24,358 --> 00:47:26,159 So that's look up number. Last but not least, 951 00:47:26,159 --> 00:47:29,219 I wanna display all the phone numbers at the end that are in the book, 952 00:47:29,219 --> 00:47:32,619 so what I'm gonna do, I can actually show you two versions of that. One version 953 00:47:32,619 --> 00:47:34,300 uses our happy friend the iterator. 954 00:47:34,300 --> 00:47:35,780 It says, "Oh, phone book. 955 00:47:35,780 --> 00:47:39,079 Give me your keyset, and from the keyset give me an iterator, and I will 956 00:47:39,079 --> 00:47:42,278 assign that to it which is an iterator of type string." 957 00:47:42,278 --> 00:47:45,938 And then I'm gonna do the loop that you just saw before. While the iterator 958 00:47:45,938 --> 00:47:49,808 has a next value, I'm gonna get its next value which is a name. All 959 00:47:49,809 --> 00:47:53,189 the iterator is just iterating over all the keys which are names in the phone 960 00:47:53,188 --> 00:47:53,848 book. 961 00:47:53,849 --> 00:47:57,709 To get the associated number, I need to say, "Phone book, get the number associated 962 00:47:57,708 --> 00:47:59,788 with that name, and then I print it out." 963 00:47:59,789 --> 00:48:01,859 Note here I don't check for an error condition 964 00:48:01,858 --> 00:48:06,659 because I know all of the names that I have are valid names in the phone book because I got 965 00:48:06,659 --> 00:48:10,058 them from the phone book, so I know when I go look them up, they'll always be there. 966 00:48:10,059 --> 00:48:13,639 And the alternative way I could've done this is using this for each syntax, 967 00:48:13,639 --> 00:48:15,650 which is just so lovely, right? 968 00:48:15,650 --> 00:48:18,860 I don't create an iterator. I just say for every name 969 00:48:18,860 --> 00:48:20,979 that's in my phone book keyset, 970 00:48:20,978 --> 00:48:24,088 get me the number by looking up that name in the phone book. 971 00:48:24,088 --> 00:48:26,009 And then I just print it out. 972 00:48:26,009 --> 00:48:29,188 So just so you believe that this actually works, 973 00:48:29,188 --> 00:48:32,998 and then we'll go in our final minute together, so we put in 974 00:48:32,998 --> 00:48:36,198 Mehran, 7236059, I won't give you my home number 975 00:48:36,199 --> 00:48:37,480 ever. Just kidding. 976 00:48:37,480 --> 00:48:40,588 I did one quarter. That was a mistake. 977 00:48:40,588 --> 00:48:41,469 Jenny, 978 00:48:41,469 --> 00:48:45,639 8675309 - I won't ask you to give me a number because it will 979 00:48:45,639 --> 00:48:49,278 be [inaudible] like random people will call you and be like, "Is this your real number?" 980 00:48:49,278 --> 00:48:51,719 Because it turned out when that song came up about Jenny, 981 00:48:51,719 --> 00:48:55,048 a lot of people called that number, and there were people who got really angry 982 00:48:55,048 --> 00:48:59,079 because some people really have that number. So we're done entering numbers, so we're done 983 00:48:59,079 --> 00:49:01,669 entering names, and we wanna look up someone. I wanna look up Mehran. Mehran's 984 00:49:01,668 --> 00:49:03,598 not in the phone book? 985 00:49:03,599 --> 00:49:06,849 Case sensitive. I need capital Mehran. 986 00:49:06,849 --> 00:49:10,150 So Mehran is in the phone book. I'm done looking folks up. And then it displays the 987 00:49:10,150 --> 00:49:14,528 phone numbers. Notice it displays Jenny first even though I entered Jenny second 988 00:49:14,528 --> 00:49:19,309 because a hashmap has no guarantee of ordering over the iterator, 989 00:49:19,309 --> 00:49:20,409 important thing to remember. 990 00:49:20,409 --> 00:49:24,269 So any questions about anything you've seen? 991 00:49:24,268 --> 00:49:25,558 All righty. Then I will see you on Friday.