1 00:00:00,180 --> 00:00:05,430 ‫In this lesson, we are going to cover hash tables and we're also going to look at dictionaries later 2 00:00:05,430 --> 00:00:05,790 ‫on. 3 00:00:05,820 --> 00:00:10,880 ‫So hash tables and dictionaries are some of the most useful collections in C sharp. 4 00:00:10,890 --> 00:00:16,470 ‫So we will see how to define them and how to use them and in which case scenario, we should take advantage 5 00:00:16,470 --> 00:00:17,010 ‫of them. 6 00:00:17,010 --> 00:00:19,580 ‫To understand the concept of hashing and tables. 7 00:00:19,590 --> 00:00:21,750 ‫First, think about a real dictionary. 8 00:00:21,750 --> 00:00:28,320 ‫So assume we have a German and English dictionary where every word in German corresponds to only one 9 00:00:28,320 --> 00:00:31,680 ‫word in English, because that would be limiting the limitation here. 10 00:00:31,680 --> 00:00:39,420 ‫So when we try and translate the word auto, which in German basically translates into car in English, 11 00:00:39,450 --> 00:00:47,820 ‫we use one word as a key word that we can search for, for example, and we got one translation as a 12 00:00:47,820 --> 00:00:48,450 ‫result. 13 00:00:48,450 --> 00:00:50,730 ‫So there is always this 1 to 1 relationship. 14 00:00:50,730 --> 00:01:00,540 ‫So we have auto and that translates into car and we can achieve exactly that also using hash tables. 15 00:01:00,540 --> 00:01:05,190 ‫And we're going to use this in a very specific example here. 16 00:01:05,580 --> 00:01:13,650 ‫So let's say we have a student registration system where every student has an ID and many other data 17 00:01:13,650 --> 00:01:18,210 ‫such as name, age, grades and picture and so forth. 18 00:01:18,210 --> 00:01:22,530 ‫For such a system to work, we will need a special ID for every single student. 19 00:01:22,530 --> 00:01:23,790 ‫So it has to be unique. 20 00:01:23,970 --> 00:01:30,750 ‫If we were to store this data using a hash table or a dictionary, the student ID would be the key and 21 00:01:30,750 --> 00:01:37,620 ‫the student data would be an object that contains all of the other student info and that would be the 22 00:01:37,620 --> 00:01:38,370 ‫value. 23 00:01:38,370 --> 00:01:42,060 ‫So we always have this key value pair. 24 00:01:43,050 --> 00:01:51,030 ‫So this is how you can find the actual data, so to speak, and this is the value that is behind it. 25 00:01:51,030 --> 00:01:53,700 ‫So you can see it like a locker. 26 00:01:53,700 --> 00:01:57,240 ‫So this is the ID of the locker or the location of the locker. 27 00:01:57,240 --> 00:02:00,140 ‫It could be different kinds of types of data. 28 00:02:00,160 --> 00:02:05,010 ‫It doesn't have to be an ID and this will be the value. 29 00:02:05,010 --> 00:02:10,710 ‫So the dictionary, they have to be of the same type in a hash table. 30 00:02:10,710 --> 00:02:12,000 ‫They can be of different types. 31 00:02:12,000 --> 00:02:15,810 ‫So this could be an integer and this could be an object, for example. 32 00:02:15,810 --> 00:02:18,480 ‫So we're going to look at hash tables first. 33 00:02:18,480 --> 00:02:25,830 ‫Therefore, we are going to create a new class or actually I have prepared a new class for this. 34 00:02:26,190 --> 00:02:27,570 ‫Let's put it down here. 35 00:02:27,570 --> 00:02:30,090 ‫And it has a couple of properties. 36 00:02:30,210 --> 00:02:34,470 ‫It has an ID property, a name property and the GPA. 37 00:02:34,950 --> 00:02:38,520 ‫And then we have a very simple constructor simply assigning the value. 38 00:02:38,520 --> 00:02:46,890 ‫So whatever ID we pass to the student object when we create it will be the ID of the student object 39 00:02:46,890 --> 00:02:47,910 ‫and so forth. 40 00:02:48,570 --> 00:02:50,070 ‫So nothing too fancy here. 41 00:02:50,070 --> 00:02:54,720 ‫We're just using a normal class as we have covered in the class chapter. 42 00:02:56,850 --> 00:03:02,730 ‫Now we could, of course, go ahead and create this class in a separate file so we could go ahead and 43 00:03:02,820 --> 00:03:05,660 ‫open up our solution explorer. 44 00:03:05,670 --> 00:03:08,790 ‫Go ahead and create a new file here. 45 00:03:08,790 --> 00:03:13,490 ‫So add new item or a new class like so and call it student. 46 00:03:13,500 --> 00:03:17,190 ‫But for this example, I'm going to keep it simple and put it into one file. 47 00:03:17,430 --> 00:03:23,010 ‫As you know, it's best practice to put it in a separate file so that you're not mixing things and you 48 00:03:23,010 --> 00:03:26,430 ‫can easier find files and classes and all of that. 49 00:03:26,970 --> 00:03:34,380 ‫Okay, so now that we have our student class, we can go ahead and create a hash table. 50 00:03:34,980 --> 00:03:41,430 ‫So inside of our program in here, in the main method, that's where I'm going to define a new hash 51 00:03:41,430 --> 00:03:46,470 ‫table and it will be a like so hash table. 52 00:03:46,950 --> 00:03:51,750 ‫And I'm going to call this one table, which will be new hash table. 53 00:03:52,920 --> 00:03:59,490 ‫The hash table class is inside of system collections, so we need to add this namespace up here using 54 00:03:59,490 --> 00:04:00,960 ‫system collections. 55 00:04:00,960 --> 00:04:07,350 ‫So hash table and it's different to the hash tables namespace because that's how I called my, which 56 00:04:07,350 --> 00:04:14,580 ‫might confuse our ID here if we want to enter hash table without having this namespace added to our 57 00:04:14,580 --> 00:04:15,180 ‫project. 58 00:04:15,180 --> 00:04:19,290 ‫But now that we have it inside of our programs class, we can use it. 59 00:04:19,290 --> 00:04:26,880 ‫So we have this new hash table which will now be a table of data, and we can put the data that we want 60 00:04:26,880 --> 00:04:27,510 ‫in there. 61 00:04:27,510 --> 00:04:34,620 ‫So this is how you can create such a hash table, how you can initialize it with an empty hash table 62 00:04:34,620 --> 00:04:36,510 ‫object like we did here. 63 00:04:36,840 --> 00:04:42,810 ‫So now what we can do is we can assign values to this hash table, and we could even call it student's 64 00:04:42,810 --> 00:04:45,660 ‫table to make our code a little more readable. 65 00:04:45,840 --> 00:04:52,260 ‫So now we just need to create a bunch of students that we want to have in our table. 66 00:04:52,260 --> 00:04:54,690 ‫So let me add those in here as well. 67 00:04:54,690 --> 00:05:00,720 ‫I'm going to call them stud one stat, 2 to 3, step four, and they will all be new student objects. 68 00:05:00,720 --> 00:05:05,040 ‫So here this will be student with the ID, one will be Maria. 69 00:05:05,040 --> 00:05:07,030 ‫She has a GPA of 98. 70 00:05:07,030 --> 00:05:10,740 ‫Then we have JSON which will have the ID of one. 71 00:05:10,740 --> 00:05:18,360 ‫This is this ID here, the unique identifier, so to speak, and then the GPA that the students have. 72 00:05:18,360 --> 00:05:18,540 ‫Okay. 73 00:05:18,630 --> 00:05:26,610 ‫So now we have all of those students and we can assign them to our students table using the students 74 00:05:26,610 --> 00:05:27,300 ‫ID. 75 00:05:27,330 --> 00:05:37,000 ‫And the good practice would be to go ahead and stay students table dot add and here we use the student 76 00:05:37,020 --> 00:05:42,870 ‫one ID so this one here the property as the key. 77 00:05:42,870 --> 00:05:49,910 ‫So you can see here a hash table allows us to have a key of type object so it can be any type of object 78 00:05:49,920 --> 00:05:56,790 ‫as the key and it can be an object optional or knowable as the value. 79 00:05:56,790 --> 00:05:59,130 ‫So we can assign a value or it can be null. 80 00:05:59,340 --> 00:06:05,040 ‫Okay, but we're going to assign a value and that will be the student one itself like so. 81 00:06:06,180 --> 00:06:15,750 ‫So now we added our student one to our students table and we can do the same thing with the other students 82 00:06:15,900 --> 00:06:19,470 ‫and we're going to use their ID for each of them. 83 00:06:19,470 --> 00:06:23,580 ‫So let's use student's table here for all of them. 84 00:06:24,990 --> 00:06:25,290 ‫Okay. 85 00:06:25,290 --> 00:06:29,610 ‫So that's our hash table now, but how can we fetch entries? 86 00:06:29,610 --> 00:06:33,300 ‫So how can we retrieve entries from our hash table? 87 00:06:33,540 --> 00:06:35,990 ‫Well, let's look at different ways for it. 88 00:06:36,000 --> 00:06:41,370 ‫So the first thing that we can do is we can just create a student object here. 89 00:06:41,370 --> 00:06:46,110 ‫So student and this object will be called stored student. 90 00:06:47,190 --> 00:06:51,570 ‫So I'm going to get the student value from my table. 91 00:06:51,900 --> 00:06:54,660 ‫So in this case, it's the student table. 92 00:06:55,500 --> 00:06:57,840 ‫Students table like so. 93 00:06:59,180 --> 00:07:03,440 ‫And here similar to how I access it in an array. 94 00:07:03,470 --> 00:07:07,750 ‫How X is a value in an array, I can access it by an ID. 95 00:07:07,760 --> 00:07:13,970 ‫So here for example, let's say I want to get the one at ID one like so. 96 00:07:13,970 --> 00:07:21,770 ‫So here the problem is that this hash table can have values that are of type object, right, as we 97 00:07:21,770 --> 00:07:22,550 ‫saw earlier. 98 00:07:22,670 --> 00:07:29,570 ‫So here, if we go ahead and call the add method, you can see we can assign a key, but then the value 99 00:07:29,600 --> 00:07:31,160 ‫will be of type object. 100 00:07:31,160 --> 00:07:36,170 ‫And that means that this here will be of type object. 101 00:07:36,350 --> 00:07:39,710 ‫But we cannot assign just an object type to the student. 102 00:07:39,710 --> 00:07:45,830 ‫It has to be off type student because this stored student one is in fact of type student. 103 00:07:46,580 --> 00:07:52,520 ‫So we need to make sure that we cast it into a student here and then this will work. 104 00:07:52,520 --> 00:07:59,090 ‫Okay, so we're casting this object into a student because we know that inside of the student's table 105 00:07:59,090 --> 00:08:00,380 ‫at position one. 106 00:08:00,380 --> 00:08:05,090 ‫So at the ID one, the value will be in fact of type student. 107 00:08:05,900 --> 00:08:12,260 ‫So here you can get the student at ID one, you could get the student at ID two and so forth. 108 00:08:12,260 --> 00:08:16,640 ‫But that only works because we know that we are using in fact these numbers. 109 00:08:16,640 --> 00:08:21,830 ‫So if you want to get it, otherwise you could use third one by ID. 110 00:08:21,830 --> 00:08:26,870 ‫So this will now give you the value behind the student one ID. 111 00:08:26,870 --> 00:08:30,950 ‫So this will basically give you this value here or this student here. 112 00:08:35,300 --> 00:08:37,430 ‫And it's this one, to be precise. 113 00:08:37,430 --> 00:08:41,690 ‫So this one here, because that's what we are storing at this ID one. 114 00:08:42,500 --> 00:08:50,090 ‫So now we can go ahead and do something like this where we now instead of fried hello world we are going 115 00:08:50,090 --> 00:08:56,090 ‫to actually write something useful where we write the student ID, the name of the student, as well 116 00:08:56,090 --> 00:08:57,290 ‫as his GPA. 117 00:08:57,350 --> 00:09:00,890 ‫So we're going to use stored student one dot ID. 118 00:09:00,890 --> 00:09:04,640 ‫So we get his ID, we get his name and we get his GPA. 119 00:09:04,820 --> 00:09:10,640 ‫And in this case, it's her GPA and so forth, because it's Maria at position one at ID one. 120 00:09:11,230 --> 00:09:16,550 ‫Okay, so let's run this and you can see student ID is one name is Maria and GPA is 98. 121 00:09:16,820 --> 00:09:20,190 ‫So here we saw how we can create a hash table. 122 00:09:20,210 --> 00:09:22,640 ‫This is how we can store data in the hash table. 123 00:09:22,640 --> 00:09:25,100 ‫Actually, this is where we store the data in a hash table. 124 00:09:25,460 --> 00:09:29,960 ‫And then this is how we can retrieve data from a hash table. 125 00:09:30,260 --> 00:09:35,660 ‫So now let's assume we want to actually print all the data inside of our hash table and we don't have 126 00:09:35,660 --> 00:09:37,750 ‫a particular key at our disposal. 127 00:09:37,760 --> 00:09:43,460 ‫We can simply do that by getting all the keys from our hash table to solve this. 128 00:09:43,610 --> 00:09:47,810 ‫First, we have to discuss a struct called dictionary entry. 129 00:09:47,930 --> 00:09:51,500 ‫Whenever we add a new entry to our hash table. 130 00:09:51,500 --> 00:09:58,520 ‫So our key value pair, a new dictionary entry object will be created for us and it will get inserted 131 00:09:58,520 --> 00:10:05,000 ‫into our hash table, which means that a hash table is basically a collection of dictionary entries. 132 00:10:05,300 --> 00:10:13,760 ‫So using a temporary object of dictionary entry, we can go through our hash table using a for each 133 00:10:13,760 --> 00:10:14,280 ‫loop. 134 00:10:14,300 --> 00:10:21,800 ‫So let's look at this in this particular example here where I'm going to go ahead and use a for each 135 00:10:21,800 --> 00:10:22,580 ‫loop here. 136 00:10:22,970 --> 00:10:26,960 ‫And this for each loop type will be the dictionary entry. 137 00:10:26,960 --> 00:10:29,180 ‫That's the struct that I was talking about. 138 00:10:29,180 --> 00:10:31,850 ‫You can see here struct dictionary entry. 139 00:10:32,180 --> 00:10:35,660 ‫So what is the item name that I'm going to use? 140 00:10:35,660 --> 00:10:36,750 ‫I'm going to use entry. 141 00:10:36,770 --> 00:10:41,300 ‫Of course, you could call this student or whatever because you know that the entries will be of type 142 00:10:41,300 --> 00:10:46,040 ‫student, but let's say you don't know it and you can also call it entry. 143 00:10:46,040 --> 00:10:52,940 ‫And then what is the collection that we want to run this through or that we want to get the values from, 144 00:10:52,940 --> 00:10:55,100 ‫and that will be our students table. 145 00:10:55,520 --> 00:10:57,920 ‫So this one retrieve. 146 00:10:59,260 --> 00:11:03,640 ‫An individual item with known ID. 147 00:11:05,370 --> 00:11:06,300 ‫And here. 148 00:11:07,750 --> 00:11:13,570 ‫Retrieve all values from a hash table. 149 00:11:15,080 --> 00:11:15,300 ‫Okay. 150 00:11:15,350 --> 00:11:21,710 ‫So an entry inside of our hash table is of type dictionary entry, as I just said. 151 00:11:21,830 --> 00:11:24,350 ‫So this allows us now to go through this. 152 00:11:25,250 --> 00:11:30,680 ‫Table, the students table and basically display the values. 153 00:11:31,280 --> 00:11:39,320 ‫So here, because entry value will return an object, you can see here it says object. 154 00:11:39,440 --> 00:11:45,470 ‫But what we want in order to get the student details is going to be a student object. 155 00:11:45,800 --> 00:11:48,590 ‫What we'll need to do is we will need to do the following. 156 00:11:48,590 --> 00:11:52,880 ‫So we create a temporary value, which will be our entry value. 157 00:11:53,180 --> 00:12:01,640 ‫Now we have the same problem that we had up here where we need to cast this into a student because it's 158 00:12:01,640 --> 00:12:02,630 ‫just an object, right? 159 00:12:02,630 --> 00:12:08,420 ‫But we want it to be of type student so that we can access the student properties. 160 00:12:08,990 --> 00:12:17,420 ‫So because otherwise entry value dot, it doesn't have ID here, I can't access its ID, I can't access 161 00:12:17,420 --> 00:12:21,260 ‫its name, all of that stuff even though it exists in there. 162 00:12:21,260 --> 00:12:29,870 ‫But our ID just doesn't allow that because we first need to make sure that we convert or cast the value 163 00:12:30,320 --> 00:12:34,130 ‫into the right type, which is going to be our type student. 164 00:12:34,130 --> 00:12:40,280 ‫So now we're casting it, we're storing it temporarily, and now we can go ahead and write the individual 165 00:12:40,280 --> 00:12:44,840 ‫values to our console, for example, or using our program however we will want. 166 00:12:44,840 --> 00:12:51,830 ‫So here we can use the student ID by using template ID because temp now is of type student and student 167 00:12:51,830 --> 00:12:59,600 ‫has ID name GPA and we can access all of those to now iterate through the entire list of all of our 168 00:12:59,600 --> 00:13:00,320 ‫students. 169 00:13:00,500 --> 00:13:02,210 ‫So let's do that real quick. 170 00:13:02,210 --> 00:13:07,760 ‫Let's run our code and see what this for each loop does for us so you can see it goes through it. 171 00:13:08,000 --> 00:13:08,600 ‫It says. 172 00:13:08,600 --> 00:13:09,750 ‫STUDENT four Steve. 173 00:13:09,770 --> 00:13:10,850 ‫STUDENT three Sclera. 174 00:13:10,850 --> 00:13:14,210 ‫STUDENT two is Jason and one is Maria. 175 00:13:14,300 --> 00:13:20,990 ‫So you see it goes from back to well, it starts at the very last entry and goes up to the first entry. 176 00:13:24,710 --> 00:13:30,920 ‫Now, that is if at this point we don't know if it's a student, even though we are converting it into 177 00:13:30,920 --> 00:13:32,020 ‫a student already. 178 00:13:32,030 --> 00:13:39,700 ‫This is really just to show you that a hash table is really just a collection of dictionary entries. 179 00:13:39,740 --> 00:13:44,390 ‫So let's look at how we can simplify this for each loop. 180 00:13:44,660 --> 00:13:51,620 ‫We can simplify it by actually saying for each where the variable type is going to be a student. 181 00:13:51,980 --> 00:13:55,130 ‫The item name, well, you could call it student. 182 00:13:55,130 --> 00:13:58,010 ‫I'm just going to call it value like. 183 00:13:58,010 --> 00:13:59,300 ‫So that's fine. 184 00:13:59,570 --> 00:14:06,190 ‫And the collection that I want to go through is going to be table while students tables dot values. 185 00:14:06,200 --> 00:14:11,660 ‫So there is this AI collection of hash table values. 186 00:14:11,750 --> 00:14:15,420 ‫So gets an AI collection containing the values in the hash table. 187 00:14:15,440 --> 00:14:21,350 ‫So this allows us to go through the values directly without having to. 188 00:14:22,150 --> 00:14:27,970 ‫Basically do what we did here, where we converted it or where we cast it into a student object. 189 00:14:28,420 --> 00:14:30,580 ‫So now we can just go ahead and. 190 00:14:31,350 --> 00:14:33,120 ‫Do the same thing as we did here. 191 00:14:33,120 --> 00:14:41,370 ‫But now, of course, it's going to be value ID and so forth because it's not entry and it's not temp, 192 00:14:41,370 --> 00:14:46,860 ‫but it's going to be in fact value because that's what this for each group. 193 00:14:47,700 --> 00:14:52,980 ‫Is giving us for each iteration of it, where it goes through all of the values that we have. 194 00:14:52,980 --> 00:14:59,310 ‫And value is going to be the individual value that we're currently looking at for that particular iteration. 195 00:14:59,580 --> 00:15:02,460 ‫So in the first iteration, it will be. 196 00:15:03,430 --> 00:15:04,110 ‫Steve. 197 00:15:04,120 --> 00:15:06,460 ‫Then it will be Clara, then Jason, then Maria. 198 00:15:07,510 --> 00:15:15,520 ‫So if we get rid of this line here or it is for each loop and we run it again, we will see that we 199 00:15:15,520 --> 00:15:17,200 ‫get the same results. 200 00:15:17,200 --> 00:15:20,230 ‫So Steve, Clara, Jason and Maria. 201 00:15:20,890 --> 00:15:21,160 ‫Okay. 202 00:15:21,160 --> 00:15:26,920 ‫So that was a quick introduction into hash tables and the next video, I'll have a little challenge 203 00:15:26,920 --> 00:15:33,550 ‫for you where you will basically extend your code a little bit to understand hash tables a little better. 204 00:15:33,550 --> 00:15:35,560 ‫And then we're going to look at dictionaries.