 Hey everybody, this is Brian and welcome to the 28th Java tutorial. Today we're going to be covering the hash table. This was a viewer request. I think it was NaturalPsychic123 that requested this. Alright, so what's a hash table? You can see how it implements a map and you know what a map is, it has a key value pair. So why is this different than a map? Well, a normal map, you have a key and a value and it will reiterate over each item going, are you the specific key? Yes, no. Are you the specific key? Yes, no. And it will go over each item. Well, a hash table takes the key and if you kind of drill through the API here, it will take the key and it will turn it into a hash code. Now what's a hash code? If you just look up MD5 on Wikipedia, you can see that it is a hash. Now what's a hash? A hash is a representation of a value. For example, let me scroll down here. Here's an algorithm. It does, it takes the value you have and it flips the bits around and it figures out through these very impressive functions and it generates something. For example, if you do an MD5 hash of the quick brown fox jumps over the lazy dog, the MD5 hash is this guy right here whereas the MD5 hash of nothing would be this guy right here. So that's a hash. Basically, it acts as an index in the hash table. It allows it to quickly look something up. For example, if you've got 10 items and you're looking for item number 8, instead of going through each item until it hits number 8, it jumps to number 5 and says, are you greater than or less than the current index? Well, 8 is greater than 5, so keep going. So then it will slice it in half again and you get the picture. It narrows it down very quickly and then once it gets within a range, it will rate it through looking for the specific index. So without further ado, let's actually just jump right in here and create a hash table. You see I've already got an import here and I'm going to just make a hash table. We have to have a key and we'll actually make our key a string and we'll make our value a string and we'll just stick with employees because we know what that is. And we'll say new hash table, default constructor, all right. So nothing new here so far, it follows the same structure that we're used to in Java where you have generics. You have a key and a value pair. That's because it has the map interface and because this is a map, we have to put and we will say 1 and we'll call this Bob, oops, not Bob, sorry, and all right, let's copy and paste a few of these, we'll say one and a half, there we go. We'll say Bob, we'll say Chad, Mark, and just Amy. I'm having a hard time coming up with random names here, but you get the drift and then we will say, we want to print these out so we're going to say for each string, we'll call it item, oops, in and we'll say, employees, key set and if you're wondering what a key set is, it's an actual set. We've gone over sets before, remember a set cannot have multiple items of the same value. That's why they're keys. Now one thing you should know, and let's just do a system out print line, is that these are being hashed. It's not storing one, five, three. It's also storing a third value, that hash, that MD5 we talked about. Now I don't know specifically if Java's using MD5 or SHA1, but you should just know it's storing a third value out there as a hash. Run this, you see one, five, three, one and a half, those are our keys. Now let's just add another one here, see what happens, and we'll call that Bobby. What do you think is going to happen if we run this? We get two keys of the same type. Well, you see how it just overwrote it. That's because if the key already exists, it's just going to simply overwrite that value. So if we actually take this one step further and say we want to do the employees and you get the actual value, and let's just say ITM, because that's our key, and let's just do this little copy and paste magic here, whoops, copied the wrong thing. Sorry, my mouse is having technical difficulties today. So we're just going to print out the key and then the actual value of the key. Now notice we have one Bob, one Bobby, which one's going to print out? Let's run this and find out. You see one is now Bobby. You also notice how they're not in the same order that we put them in there. Why is that? Well, that's because of that MD5 hash or whatever algorithm it's using. It's ordering it based off the hash value. So in memory, this may look something like, you know, 002, and I think Mark was one, so it'd be like 01. That's why they're not in the same order that you just put them in there, because it's sorting them based off the hash of the value. So you may get some unpredictable results if you're using the hash table, but it's important to understand that it's looking at the hash, not the actual value of the key that you put in here. So I know it's confusing. I recommend that you actually go out and you read up on hashes and hash tables because they're in almost every single language because they are just incredibly efficient at looking things up. So this is it, Brian. Thank you for watching. I hope you found this video educational and entertaining.