‫Instructor: Hi, within this lecture we are going to learn
‫a new data structure called HashMap.
‫So, it's written like this HashMap.
‫So, this is a data structure
‫that we store values with key and value pairings.
‫So, if write HashMap, you can see K and V over here.
‫So, if you write array list,
‫you only see one parameter like this.
‫Okay, we only write the type
‫of the values that we are going to store over here.
‫But when we write HashMap,
‫you see two parameters with a comma in the middle.
‫And this K and V stands for key and value.
‫So, we are storing key and value pairings over here.
‫You are gonna see what I mean in a minute.
‫For example, we can store string to string key and values.
‫So, I'm gonna call this my HashMap,
‫and I'm going to initialize it like this,
‫as we have done before.
‫It's no different than sets.
‫It's no different than array list,
‫but we are not storing only one value.
‫For example, if you say my HashMap dot put,
‫rather than add, okay?
‫It will ask you for a key and the value
‫and the type of the key and the type of the value
‫is strength, because we have defined it so.
‫So, we are gonna give two strengths.
‫In order to write strengths,
‫of course we are going to add some quotation marks, right?
‫That's how we write texts.
‫If we had put a string to integer, for example,
‫then it would ask for string and then integer.
‫And you are gonna see that example as well, don't worry.
‫Right now we are putting some keys and values.
‫For example, I can say key name and value will be James.
‫Now, I can put something else
‫like a surname or instrument, for example.
‫And I can say, yeah, James plays guitar.
‫So, this is ideal when you want to label the value
‫that you're going to be storing.
‫For example, I can just write print line
‫my HashMap dot get.
‫And this time I'm not gonna give any indexes,
‫because it doesn't have any index,
‫it only has keys and values.
‫Now, you can get the name and instrument like this.
‫It has to be exactly the same thing
‫that you have written before.
‫For example, we see James, and we see guitar.
‫So, that's how you store values with HashMaps.
‫And that's how you get values with HashMaps.
‫So, rather than name, you can just call
‫it whatever you want, okay?
‫And you can do it not only with strings
‫but Booleans and integers and everything as well.
‫For example, if you want to store a string to integer,
‫you're gonna have to write it like this.
‫I'm gonna show you.
‫Let's call this my second HashMap, okay?
‫And I'm going to initialize this as usual, okay?
‫New HashMap, but as you can see, we get an error.
‫So, it says that this type argument cannot
‫be a primitive type.
‫So, what does it even mean, a primitive type?
‫We haven't seen it.
‫Actually, we have seen it in an unconscious way,
‫because we have been creating integers,
‫and we have been creating non primitive types
‫and primitive types as well.
‫For example, when I created an integer before,
‫I followed this convention.
‫But if you come over here like to a string,
‫you can see it starts with a capital letter A.
‫Array list starts with a capital uppercase A.
‫Like HashSet starts with a capital H.
‫HashMap starts with a capital H.
‫So, why does integers start with lowercase i?
‫Because it's a primitive type,
‫you can actually create integer with uppercase I, as well.
‫It will drive from the class rather
‫than a primitive type like this.
‫So, you can write integer,
‫my integer is 10, for example.
‫And it's the same thing, but if you do it
‫with a primitive type like this,
‫int then it has some less memory consumption, okay?
‫But you cannot do that with HashMaps.
‫You have to specify the class name like this.
‫So, this will be a string to integer.
‫And you can add a new thing to the new element
‫to your HashMap by saying put, and as you can see,
‫it asks for a string and an integer right now,
‫and you can add whatever you want in here right now
‫as long as you provide a string and an integer.
‫And the string will be our key,
‫like running 100 calories.
‫Like think of this as a fitness app
‫for example, basketball, I dunno, 200 calories.
‫So, rather than storing this in an individual array,
‫now, we are storing all of them
‫into same data structure which is a HashMap.
‫And I can get the calories of running
‫anytime I want by calling run keyword.
‫So, this is how you work with HashMaps, okay?
‫So, we're gonna stop here and continue
‫within the next lecture with different
‫kind of methods in Java fundamentals.