There are collection types that implement the map interface.
For example, a hash map directly implements the map interface.
Linked to hash map as a child class of hash map, which implements the map interface.
Tree map implements these sort map interface, which in turn implements the map interface.
So one way or another, all three classes implement the map interface, whether it's directly or indirectly.
In this lesson, you're in a see polymorphism from the lens of map.
Open the folder for this lesson by following this path in your course resources.
In our map is an interface defines methods that a class must implement.
The hash map class implements this interface, it also implement a bunch of other interfaces, but namely
map.
Here are some of the methods that map the phones.
Once again, the familiar sighs method hash map implements the interface, so we know the hash map class
overrides this method.
You can go back here and call size from the hash map.
Here is another one is empty, and it follows that we can call is empty.
Contains key, and so we know hash map already overrides contains key, which we can use to check for
a particular key.
Anyways, the hash map class implements this interface, so it's basically signing a contract that it's
going to override all of the methods inside the interface.
And it does.
For example, we know our hash map is going to be forced to override or remove books that remove.
And yeah.
So a separate hash map from link to hash map and tree map.
The most striking difference is that hash map has no order length.
Hash map and tree map do follow in order.
If you print the hash map, you'll notice that they appear in a random order.
Tree map entries are sorted according to the compare to method.
Based on the book classes compared to Method, we can expect tree map to sort its entries from lowest
the highest price.
So declare this as a true map instead.
And check it out, all of the entries inside the tree map are sorted from lowest to highest price.
Now for a link to hash map, the entries appear according to their insertion order.
If I declare this as a linked hash map.
And run the app.
The entries appear according to the order that you inserted them in.
So tree map and link to hash map, follow and order hash map doesn't have an order.
Here I can convert this to a key set.
And to an array.
And grabbed the object index to.
And I know it's going to be Game of Thrones.
If I make this into a tree map.
I know in next two is going to return Harry Potter, because that's the way it's going to be sorted.
But if we use a hash map.
I have no idea what in next two is going to give me because everything is going to be out of order.
Random.
So when do you use a map, when there is parity between data?
But specifically, when do we use a hash map?
If you don't care about order, use a hash map because it offers the best performance.
Use a tree map if you need the entries to be sorted, according to what you specify.
Use a linked hash map if you need the entries to be sorted according to their insertion order.
Now, it's worth noting that I always use hash mark because it offers the best performance out of the
three, and it's quite rare that indeed the entries to be sorted.
And in this course, we're probably going to keep using hash map.
Now, it's interesting, as you remember, that interfaces allow for polymorphism, why is polymorphism
important, as always, flexibility.
In this case, print map expects a map.
And our hash map can take the form of a map so we can directly pass it in.
We can even go a step further and declare this as type map.
And even here, I can define this as a trauma.
And what I have to change anything because I know Trump can take the form of map once again.
I can define this as a linked hash map and everything should still work.
So once again, we see that polymorphism helps make our code reusable because all three of our collections
share one common form.
Use a map collection when there is parity between data.
We can drill further and say, use a hash map if you don't care about order because it offers the best
performance.
Use a tree map if you need the entries to be sorted, according to what you specify.
Use a linked hash map if you need the entries to be sorted according to their insertion order.
In this course, we're likely going to keep using hash map because it offers the best performance out
of the three, and it's not common that we're going to need the entries to be sorted.