The hash map is another useful collection type.
Array List and Hashmat are collection types, the main differences, every entry in an array list is
one value.
Every entry and a Hashmat is a pair of data.
In this lesson, you will learn to use Hashmat.
Inside your Java bootcamp, resources open up the folder for this lesson by following this path.
A hash map stores key value pairs.
The hash map is useful when there's parity between data, every entry and a hash map is a key value
pair.
Every key is like a variable that points to a value, for example, short is 499.
Pens are twelve ninety nine and shoes are twenty three ninety nine.
To create a hash map, you need to write the class type hash map and inside a generic the find that
type of each key, the type of each value.
And remember that generics do not accept primitive types.
And the third step is to make space for a new object of the heightmap class.
All right, inside key values Java, there are two arrays, one is an array of vegetables, the other
is an array of prices.
There is parity between each element in both the race.
So instead of having to erase create a new object of the Hashmat class.
Every key in the hash map is a string, and each value is going to be a double.
And I'm going to call the heightmap items.
Hash maps have a method called put, and you can use it to put an entry into the hash map.
Inside main, we're going to put three entries into the hash map.
The first entry we're going to put is cauliflower with a price of four point ninety nine.
This looks a lot better to me.
I'll play some breakpoints and run the debugger.
And as you can see, every entry in the hash map is a key value pair.
Hazmats also have a method.
Get allows you to access a value from the hash map using its key.
So inside mean I can get the price of cauliflower.
I can get the price of spaghetti squash and I can get the price of parsley.
I'll print each price.
And run the code.
And as you can see, get returns, the price of each key.
Hash map entries don't have an order when you add a new entry, it's placement is random.
That's why house maps don't have an index number, you can't index a value in a Hashmat.
You can only get a value using its key.
So inside key values that Java, I can run through every entry in the hash map using for each.
For each expecta lambda expression, the lambda for this, for each receives every key value pair.
And once again, an arrow points to a block of code where we can just print.
Kee Colon.
Followed by the value.
You'll notice they appear in a random order, we added spaghetti squash second, but it appears last.
Put can add or update an entry.
It adds an entry if the key doesn't already exist.
It updates an entry if the key already exists.
Inside Kivel Java, if you add another entry where the key is partially.
It's just going to update the price for Pursley.
There you go.
Now it's three ninety nine.
Let's recap in this lesson, you learn to create a hash map.
And a hash map is useful when there's parity between data, every entry and a hash map is a key value
pair.
You can use the put method to add a new entry or update an existing one.
Finally, hash maps don't have an order, so you can't index a value from a hash map, but you can use
the get method to get a value based on its key.