HashMap Java Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hashmaps were one of those topics that were pretty confusing to me even just the name itself hashmap like how am I supposed to know what that does well it turns out that hash maps are nothing more than a list of keys and values and different keys have different values this is my hash map Java tutorial hey what is up it's Alex back again helping you learn Java on this channel I make a new Java tutorial that's fresh and new and exciting that you can check out every single week so if you're new here then consider subscribing so let's kick it off by going into our program editor I'll just go to file new Java project hash map cuts tutorial and then open it up right click on source new class call it hash map awesome this awesomeness public static void they finished Java is really great at storing data like we can store integer variables like this into a equals 10 we can store string variables like this string b equals hello and we could store a really long list of data too so we can have an array of numbers c and then set that equal to some list of numbers 1 2 3 and this solves the problem of storing one value into one variable named a is 10 b is hello c is a list of 1 2 3 but what would we do if we had 1 million of these variables say we had a bunch of integers like we have another integer B that's 3 we have another integer see that's like 88 how could we store a is 10 B is 3 C is 88 into 1 easily accessible place this is where hashmaps come in the word hash map itself was a little confusing to me and I honestly don't know why it's called that but I'm going to show you what it is and how you can use it so to store our a b and c into a hash map we would just type hash map with a capital H and M we'll name it - like happy because it makes us very happy that we're finally getting to learn when hashmaps are and set that equal to new hashmap with parenthesis and a semicolon hover over the name hashmap and click this import hashmat Java util this will auto generate this import statement at the very top and just tells Java to bring in the code for a hatch map so we can use it to put a b and c and their corresponding values into happy we just type the name of the hashmap we're just happy type a dot to bring up everything that this hash map can do and this can do quite a bit of things for us but we're gonna say put and hit enter it auto generated some stuff for us but i'm just gonna delete that where ki was I'm gonna put the string inside of double quotes and I'm going to put the integer amount 10 into the second one after the comma and then I'm gonna put a semicolon now inside of happy there's an an e that's equal to 10 I'm gonna do the other ones and then I'm gonna show you more of how it works so we'll do happy dot put B is value three and happy dot put C is value 88 you'll notice that there are a lot of yellow underlines here these first three are just saying that a B and C aren't being used but that's okay these ones if you hover over it it says hash map is a raw type references to generic type hash map Cavey should be parameterised this just means it wants to know the type of the key and the value what's on the left side of the parenthesis is the key or the name and on the right is what is stored into it so to tell it what type our values are we put some less than and greater than sign and then our first one is a string since it's in double code so we'll say string and a comma and the second one is and integer so we say capital integer it has to be capital integer and not lowercase integer because this is a primitive type it turns purple it's a key word and to say the actual type we have to uppercase it it's kind of confusing but this will get it to work for you finally we could just copy this and put it over on this side so now it knows that every key is a string and every value is an integer let's see that this is working by printing out our hash map go to system dot out dot print line and just type the name of our hash map save it and run it and we will see a is equal to 10 B is equal to 3 and C is 88 which is pretty awesome everything that we did happy put as the key on the left so a and the value on the right 10 so if we want to get the value of C I'm just going to go in here we're happy is I'm going to have a dot and we can get the value of C by typing get and replace key with C save it and run it you'll see that it returns 88 which is what we have here now let's do another example where we do some more advanced things to our hash map let's make it by just typing hash map well name it fun and say equals new hash map with our parentheses and semicolon now let's do like a user and a password which are two strings user is usually a string password usually a string and these yellow underlines are saying it wants to know exactly what type we know that so we'll just type string comma string now I'll do the same over here I'll just make this a little wider so you can see more to add our users and passwords is super easy we just type the new or SMAP a dot and then put will say our first user is um Bobby Jo 1996 and the password name is fluffy ponies like that now we'll add another user func put Hello Kitty fan 21 value and put one more pass phrase like password once you three we print out everything just typing turned out fun just down with an E save it running and we get it just like before except now it's with two strings you can remove elements from a hashmap by typing the name of the hashmap dot remove and then type the name of in this case the user so let's say we want to remove Hello Kitty fan 21 just copy type that into there save it and run it and now you see when we print out everything that's no longer in fun the user Hello Kitty fan 21 and the password are no longer in the hashmap what you can also do is see if a hashmap contains a certain value so you can say fun dot contains value and we'll see if it has the value password 1 2 3 in there let's paste that in there save it and it returns true if it is in there and false if it's not in there so in this case it is in there but if I said password 1 2 4 then it would be false notice how if I do cool guy swag into here and save it and run it it still brings back false because this is saying if fun contains the value and the values are always on the right but cool guy swag is on the left that's actually a key but we can change this to contains key save it and run it in that war returned true this is really useful inside an if statement so you can say if fun dot contains key cool guy swag then we know the cool guy swag users in there and we can do different things you can also get the size and other things like that so we'll do fun dot size and we get three you can even replace so say a user changes a password we could do fun that replace will say Bobby Joe 1996 will place his password with better password like that if we run it and print out fun you'll see that this first statement fun that replace returns the old value fluffy ponies right here but when we print out everything you'll see that Bobby Jo 1996 has his new password notice how the order is different this is a big difference between hash maps and ArrayList hashmaps don't really have an order can't rely on the specific order of a hash map only through the keys and values but an ArrayList you can get the certain index based on its position well you can also do is get all of the values of being fun dot Val use let's delete that last one we get all the values and you can do the same with keys but doing key set computer science classes really like hash maps because it sort of tests your brain knowledge of how these data structures are working they're really confusing to me just just from the name hash map alone honestly but they're really not as bad as you think it's just a long list of things on the left that are set to the thing on the right and you can access all of them by doing hashmat dot what I showed you before so if you enjoyed this hashmap java tutorial let me know by leaving a like and i'll see you all in the next video [Music]
Info
Channel: Alex Lee
Views: 207,008
Rating: 4.9578948 out of 5
Keywords: hashmap java, java hashmap, hashmaps java, hashmap in java, hashmap java example, java hashmap tutorial, hashmap java tutorial, hashmap, hashmap tutorial in java, hashmaps, java (programming language), map interface, collections in java, hashmap internals, hashmap vs hashtable, java hashmap explained, hash map, how hashmap works, hash map in java 8, java interview questions and answers
Id: 70qy6_gw1Hc
Channel Id: undefined
Length: 11min 42sec (702 seconds)
Published: Thu May 23 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.