1 00:00:00,180 --> 00:00:03,960 ‫Instructor: Hi, within this lecture we are going to learn 2 00:00:03,960 --> 00:00:07,320 ‫a new data structure called HashMap. 3 00:00:07,320 --> 00:00:09,783 ‫So, it's written like this HashMap. 4 00:00:10,950 --> 00:00:13,020 ‫So, this is a data structure 5 00:00:13,020 --> 00:00:18,020 ‫that we store values with key and value pairings. 6 00:00:18,300 --> 00:00:23,280 ‫So, if write HashMap, you can see K and V over here. 7 00:00:23,280 --> 00:00:25,740 ‫So, if you write array list, 8 00:00:25,740 --> 00:00:28,500 ‫you only see one parameter like this. 9 00:00:28,500 --> 00:00:30,667 ‫Okay, we only write the type 10 00:00:30,667 --> 00:00:34,470 ‫of the values that we are going to store over here. 11 00:00:34,470 --> 00:00:37,026 ‫But when we write HashMap, 12 00:00:37,026 --> 00:00:42,026 ‫you see two parameters with a comma in the middle. 13 00:00:42,360 --> 00:00:46,680 ‫And this K and V stands for key and value. 14 00:00:46,680 --> 00:00:50,430 ‫So, we are storing key and value pairings over here. 15 00:00:50,430 --> 00:00:52,920 ‫You are gonna see what I mean in a minute. 16 00:00:52,920 --> 00:00:57,920 ‫For example, we can store string to string key and values. 17 00:00:57,960 --> 00:01:00,300 ‫So, I'm gonna call this my HashMap, 18 00:01:00,300 --> 00:01:03,210 ‫and I'm going to initialize it like this, 19 00:01:03,210 --> 00:01:04,883 ‫as we have done before. 20 00:01:04,883 --> 00:01:06,810 ‫It's no different than sets. 21 00:01:06,810 --> 00:01:08,850 ‫It's no different than array list, 22 00:01:08,850 --> 00:01:11,782 ‫but we are not storing only one value. 23 00:01:11,782 --> 00:01:16,020 ‫For example, if you say my HashMap dot put, 24 00:01:16,020 --> 00:01:18,570 ‫rather than add, okay? 25 00:01:18,570 --> 00:01:21,810 ‫It will ask you for a key and the value 26 00:01:21,810 --> 00:01:24,900 ‫and the type of the key and the type of the value 27 00:01:24,900 --> 00:01:29,070 ‫is strength, because we have defined it so. 28 00:01:29,070 --> 00:01:32,150 ‫So, we are gonna give two strengths. 29 00:01:32,150 --> 00:01:33,870 ‫In order to write strengths, 30 00:01:33,870 --> 00:01:38,870 ‫of course we are going to add some quotation marks, right? 31 00:01:39,300 --> 00:01:41,460 ‫That's how we write texts. 32 00:01:41,460 --> 00:01:45,150 ‫If we had put a string to integer, for example, 33 00:01:45,150 --> 00:01:48,810 ‫then it would ask for string and then integer. 34 00:01:48,810 --> 00:01:52,080 ‫And you are gonna see that example as well, don't worry. 35 00:01:52,080 --> 00:01:55,680 ‫Right now we are putting some keys and values. 36 00:01:55,680 --> 00:02:00,680 ‫For example, I can say key name and value will be James. 37 00:02:00,870 --> 00:02:03,090 ‫Now, I can put something else 38 00:02:03,090 --> 00:02:07,170 ‫like a surname or instrument, for example. 39 00:02:07,170 --> 00:02:10,500 ‫And I can say, yeah, James plays guitar. 40 00:02:10,500 --> 00:02:14,520 ‫So, this is ideal when you want to label the value 41 00:02:14,520 --> 00:02:16,953 ‫that you're going to be storing. 42 00:02:17,970 --> 00:02:20,520 ‫For example, I can just write print line 43 00:02:20,520 --> 00:02:23,310 ‫my HashMap dot get. 44 00:02:23,310 --> 00:02:26,460 ‫And this time I'm not gonna give any indexes, 45 00:02:26,460 --> 00:02:30,330 ‫because it doesn't have any index, 46 00:02:30,330 --> 00:02:34,200 ‫it only has keys and values. 47 00:02:34,200 --> 00:02:37,920 ‫Now, you can get the name and instrument like this. 48 00:02:37,920 --> 00:02:40,350 ‫It has to be exactly the same thing 49 00:02:40,350 --> 00:02:42,300 ‫that you have written before. 50 00:02:42,300 --> 00:02:46,350 ‫For example, we see James, and we see guitar. 51 00:02:46,350 --> 00:02:50,100 ‫So, that's how you store values with HashMaps. 52 00:02:50,100 --> 00:02:53,054 ‫And that's how you get values with HashMaps. 53 00:02:53,054 --> 00:02:56,678 ‫So, rather than name, you can just call 54 00:02:56,678 --> 00:02:58,800 ‫it whatever you want, okay? 55 00:02:58,800 --> 00:03:02,520 ‫And you can do it not only with strings 56 00:03:02,520 --> 00:03:06,120 ‫but Booleans and integers and everything as well. 57 00:03:06,120 --> 00:03:11,005 ‫For example, if you want to store a string to integer, 58 00:03:11,005 --> 00:03:13,729 ‫you're gonna have to write it like this. 59 00:03:13,729 --> 00:03:14,562 ‫I'm gonna show you. 60 00:03:14,562 --> 00:03:16,973 ‫Let's call this my second HashMap, okay? 61 00:03:16,973 --> 00:03:21,973 ‫And I'm going to initialize this as usual, okay? 62 00:03:21,990 --> 00:03:26,129 ‫New HashMap, but as you can see, we get an error. 63 00:03:26,129 --> 00:03:29,790 ‫So, it says that this type argument cannot 64 00:03:29,790 --> 00:03:32,190 ‫be a primitive type. 65 00:03:32,190 --> 00:03:35,310 ‫So, what does it even mean, a primitive type? 66 00:03:35,310 --> 00:03:37,051 ‫We haven't seen it. 67 00:03:37,051 --> 00:03:41,190 ‫Actually, we have seen it in an unconscious way, 68 00:03:41,190 --> 00:03:43,890 ‫because we have been creating integers, 69 00:03:43,890 --> 00:03:46,530 ‫and we have been creating non primitive types 70 00:03:46,530 --> 00:03:48,780 ‫and primitive types as well. 71 00:03:48,780 --> 00:03:51,690 ‫For example, when I created an integer before, 72 00:03:51,690 --> 00:03:54,030 ‫I followed this convention. 73 00:03:54,030 --> 00:03:58,290 ‫But if you come over here like to a string, 74 00:03:58,290 --> 00:04:01,653 ‫you can see it starts with a capital letter A. 75 00:04:03,105 --> 00:04:05,280 ‫Array list starts with a capital uppercase A. 76 00:04:05,280 --> 00:04:08,820 ‫Like HashSet starts with a capital H. 77 00:04:08,820 --> 00:04:11,580 ‫HashMap starts with a capital H. 78 00:04:11,580 --> 00:04:16,580 ‫So, why does integers start with lowercase i? 79 00:04:16,680 --> 00:04:19,260 ‫Because it's a primitive type, 80 00:04:19,260 --> 00:04:24,090 ‫you can actually create integer with uppercase I, as well. 81 00:04:24,090 --> 00:04:26,760 ‫It will drive from the class rather 82 00:04:26,760 --> 00:04:29,340 ‫than a primitive type like this. 83 00:04:29,340 --> 00:04:31,545 ‫So, you can write integer, 84 00:04:31,545 --> 00:04:34,620 ‫my integer is 10, for example. 85 00:04:34,620 --> 00:04:37,110 ‫And it's the same thing, but if you do it 86 00:04:37,110 --> 00:04:39,060 ‫with a primitive type like this, 87 00:04:39,060 --> 00:04:44,060 ‫int then it has some less memory consumption, okay? 88 00:04:45,339 --> 00:04:49,080 ‫But you cannot do that with HashMaps. 89 00:04:49,080 --> 00:04:52,620 ‫You have to specify the class name like this. 90 00:04:52,620 --> 00:04:55,713 ‫So, this will be a string to integer. 91 00:04:56,640 --> 00:05:01,640 ‫And you can add a new thing to the new element 92 00:05:02,580 --> 00:05:05,760 ‫to your HashMap by saying put, and as you can see, 93 00:05:05,760 --> 00:05:09,090 ‫it asks for a string and an integer right now, 94 00:05:09,090 --> 00:05:13,950 ‫and you can add whatever you want in here right now 95 00:05:13,950 --> 00:05:18,330 ‫as long as you provide a string and an integer. 96 00:05:18,330 --> 00:05:21,270 ‫And the string will be our key, 97 00:05:21,270 --> 00:05:24,150 ‫like running 100 calories. 98 00:05:24,150 --> 00:05:28,290 ‫Like think of this as a fitness app 99 00:05:28,290 --> 00:05:33,290 ‫for example, basketball, I dunno, 200 calories. 100 00:05:33,510 --> 00:05:38,010 ‫So, rather than storing this in an individual array, 101 00:05:38,010 --> 00:05:39,720 ‫now, we are storing all of them 102 00:05:39,720 --> 00:05:43,233 ‫into same data structure which is a HashMap. 103 00:05:44,316 --> 00:05:46,710 ‫And I can get the calories of running 104 00:05:46,710 --> 00:05:51,540 ‫anytime I want by calling run keyword. 105 00:05:51,540 --> 00:05:56,190 ‫So, this is how you work with HashMaps, okay? 106 00:05:56,190 --> 00:05:58,650 ‫So, we're gonna stop here and continue 107 00:05:58,650 --> 00:06:00,720 ‫within the next lecture with different 108 00:06:00,720 --> 00:06:03,873 ‫kind of methods in Java fundamentals.