A Java HashMap is a data structure that stores key-value pairs, allowing efficient retrieval of values by their associated keys; it provides methods like put() for adding entries, get() for retrieving values, remove() for deleting entries, containsKey() and containsValue() for checking existence, and clear() for removing all entries, with the size() method returning the number of key-value pairs, and iteration possible through keySet() for displaying all entries in an organized format.
Java HashMap Tutorial: Key-Value Pairs and Methods Explained
Added:hey I was it going everybody its bro here hope you're doing well and in this video we're going to be discussing hash maps in Java and at the end of this video we're going to be creating a hash map of countries and capitals and we're going to be displaying it in a neat organized table so let's get into it before you reach the end of this video make sure to LIKE comment and subscribe so that we together can challenge and defeat the mighty YouTube algorithm what is going on everybody so for this video we're gonna be discussing hash maps so hashmaps are very similar to array lists they store a series of objects kind of like a normal array but there are a lot more user friendly but the difference between a hash map and an ArrayList is that hash maps can store key value pairs so for example what might come to mind is a name and an email it's going to store a pair of information a key value pair so with a name there's usually an associated email or vice-versa or a username and an associated ID and for the example we're going to be working on for this video is that we're going to create key value pairs of countries and their associated capitals so in order to use a hash map we need to import one thing at the top of our program so we're going to tell you import Java dot util dot hash map I don't know why but saying hash map makes me think of hash browns now to create a hash map this is what we're gonna type we're going to type in hash map make sure to get the capitalization right the H and the M are capital then we need a unique name for this hash map so I'm going to call this country's equals new hash map parentheses semicolon step two is that next to the word hash map we're going to add a set of angle brackets and then we're going to list the datatypes of the information that we're storing so if we're storing a country and an associated capital we're going to store two strings here and then make sure the S is capital now if you're storing different data types for example an integer well these hash Maps only store objects so you need to use encapsulation whoa hey guys it's bro from the future here so I actually made a mistake at this point I wrote down need to use encapsulation what I meant to write was need to use wrapper class encapsulation is something completely different from wrapper classes and I tend to mix up those two encapsulation has something to do with privacy statuses and wrapper classes that's the concept of taking a primitive data type and converting it to an object so when I say need to use encapsulation for this part I meant to say need to use in the wrapper class that is all time to go back to the future so for example if you need to store a string and a value that's an integer instead of writing int here you would write the whole word integer with a capital I if you need to store a double you type in double with a capital D or boolean with a capital B so you get the idea so we're going to store two strings both have capital S and then we're going to place this within the other set of angle brackets as well all right here is our hash map and it's called countries now we want to place some key value pairs within our hash map countries so I'm just going to add a note that says add a key and value and in order to do this this is where we're gonna type the name of our hash map countries dot put whoa there we go not sure what happened there it just freaked out so countries dot put and then we need it to values to place within here the first is going to be a country so maybe for an example let's type in USA and for the second value we will type in the capitol of the United States which is Washington DC let's add a few more countries so we're going to type in the name of our hash map dot and then use the put function not sure why he keeps freaking out like that and then we're going to add another country how about India and the capital of India is New Delhi and just to save time I'll copy these but I think you get the idea so maybe we'll add Russia and the capital of Russia is Moscow and China and the capital is Beijing okay we have our countries now we can actually display all the values all the objects within our hash map so all we need to do is type in system dot out dot print line or just print and what we're going to type in here it's just the name of our hash map all right let's try it so even though this is displaying all of the values all of the key value pairs within our hash map it's not too pretty though so we're going to be formatting this later but let me show you a few other useful functions for hash maps so I'm just going to turn this into a comment for now and let's start fresh now we can remove a country from our hash map so we're going to type in the name of our hash map so countries dot remove and then type in a key so let's say that the USA goes rogue and they are no longer considered a country all right so we're going to remove the USA and then we are going to display countries so let's see if it's still there yep and it appears that the USA is missing so let me show you guys a few other functions so we can also get a capital from one of these keys then so we're going to type in the name of our hash map again so countries dot and this one is get and then we need to add a key here so let's get Russia then I'm just going to get rid of this print line okay so this doesn't appear to do anything so what we're gonna do is put this within a print statement because right now it's returning whatever the value is for Russia so system dot out dot print or print line and then within the parentheses countries dot get Russia so this is going to return the capital of Russia which is Moscow okay moving on so let's clear everything so we're going to type countries dot clear and then we are going to display our hash map so system dot out dot print line countries and let's see if all the countries are still there which they are not everything's missing now everything's been cleared out okay moving on let's display the country's size the size of our hash map how many key value pairs we have in here so what we'll do for this is type in the name of our hash map countries dot size and then we need to put this within a print line statement I guess printer print line and let me get rid of this for now okay so this will display the size of our hash map how many key value pairs are worth in here basically and there are four one two three four that is correct let's replace one of the values for a specific aid more specifically the USA so type in the name of the hash map dot and we're going to use the replace function so we need to specify the key let's replace the value for the USA so we need to replace this with a new value so let's say that President Trump decides that the new capital of the u.s. is going to be in Detroit and then let's display our hash map so system dot out dot print line countries yep and the new capital of the USA is Detroit okay I have two last functions to show you guys so we're going to check to see if a certain key is within our hashmap and there is a specific function to use this or to do that so countries then dot then we're going to use the contains key function and we're going to look for a certain key to see if it's there let's check to see if England is in here now this returns a boolean value true or false so if we were to have it just like this this wouldn't appear to do anything so let's actually display what this returns by putting this within a print statement so system dot out dot print and then just surround this function with a set of parentheses so if England is a key within our hash map it's going to return true if it doesn't find it it returns false now let's check to see if our hash map contains a certain value so it's going to be very similar to what we had up here so I'm just going to copy and paste this but we're going to use the contains value function and let's search for perhaps Beijing so if this value is within our hash map it's going to return true if not it returns false but Beijing is a value within our hash map so it returns true okay so the last thing I'm going to show you guys is a decent way of displaying all of the key value pairs within a hash map and you can actually use a or each loop to do this so this is what we're going to type we're gonna type four then a set of parentheses string with a capital S we have to list the datatype of what we're displaying so string with the capital S we'll use I for an index then a colon and then countries dot key set so this will iterate once for each key within our hash map and let's just display system dot out dot print I to begin with and let's see what we have so far oh let's make this print line my bed okay USA China India Russia so let's also display the value as well so I'll just make another line for this system dot out dot may be print line okay if we want to display a value for an associated key what we're gonna type is countries dot get PI and let's try it now okay we're getting somewhere but I'm gonna make a few changes we're going to change the first line to just print and then I'm going to add how about a tab so we'll use the escape sequence for a tab character plus maybe an equal sign and then I'll add a space after so let's check to see this now and what we get is a somewhat decent looking table when we display all the key value pairs for our hash map so that's the basics of hashmaps if you would like a copy of all this code i'll post it in the comments down below and pin it to the top and your assignment is to create a hash map and then display all of the key value pairs and then take this display and post it in the comment section but yeah that is how hashmaps work in java hey you yeah I'm talking to you if you learn something new then you can help me help you in three easy steps by smashing that like button drop a comment down below and subscribe if you'd like to become a fellow bro [Music]
Up Next

How Google Searches Billions of Documents: Indexing & Query Explained
@TechDummiesNarendraL
196.3K views•2019-06-17

BitTorrent Protocol Explained: Piece Selection & Peer Choking
@StevenGordonAU
481 views•2013-02-22

HTTP Requests Explained: GET, POST, PUT, DELETE
@codecademy
103.1K views•2021-10-07

Enigma Machine Mechanics: WWII Encryption Explained
@JaredOwen
13.2M views•2021-12-11
Related Study Plans & Knowledge Roadmaps
Structured learning paths in Computer Science












![[자바의 정석 - 기초편] ch12-1 지네릭스란?](https://i.ytimg.com/vi/QcXLiwZPnJQ/maxresdefault.jpg)






















![Cấu trúc dữ liệu & Giải thuật [14]: #HashTable | Bảng Băm | #Set & #Map](https://i.ytimg.com/vi_webp/dMyhU07qcgs/maxresdefault.webp)



