Hello, dear students, today we have important topic we are going to review another Iraq of Java collections
framework that is MOP Iraqi today.
We are going to run map interface.
We are also going to review such interfaces like sorted map and navigable map.
I'm going to hold overview of implementations of map interface and what features they have.
Also, we'll talk about what the dictionary is and how hash tablecloth is different from hash map.
After we learn, Ceri will focus on map interface and its methods.
Your review of map interface.
We also will learn what entry type is.
As you can see that we have a lot of things to learn in our plans.
Let's start and I'd like to start with you from the overview of Map Iraqi first and the key features
of MAP.
And after we learn this, we are going to dive deeper in the details to investigate each interface in
particular on the top of our Iraqi, we have map interface.
This type exists to map keys to values.
All keys are unique, various may be duplicated.
And there are a lot of places and our programs where we can apply map implementations.
If you remember on one of the lessons I showed you example of mobile app for sports betting tips, for
example, we can receive from server a lot of sports events.
After that, we can group them by date and your keyword would be the specific date and the value would
be a collection of sporting events on this day or another example, recently you have implemented Home
Task Help Desk.
In that task.
You also could use map implementations to map support.
Request type was its priority.
In this case, support request type would be the key and it will be unique and priority would be the
value and can be duplicated.
And I keep creating examples on the fly because there really are a lot of cases where you need some
data structure where you would store key value pass.
OK, so we really understood why we need map.
Now let's understand what specific different implementations of map interface have abstract map use
implementations for some of the methods of map interface, hash map implementation of map interface.
IT stores element using the mechanism of hash tables that guarantees constant amount of time for each
region and place elements.
That's why everyone loves hash map linked hash map.
Besides storing elements also memorized the sequence of adding entries in the map.
So the order of elements during the iteration is predictable because linked hash map maintains the list
ran through all of its entries.
If we need thread safe implementation of the map, we would opt for concurrent hash map that implements
concurrent map.
That in turn extends map interface.
We have a few more interfaces that extend map interface.
Zaya sorted map and navigable map sort of map provides ordering of a map based on its case.
The map is ordered according to the natural order of its keys that is defined according to implementation
of comparable interface or alternatively, Solin order may be defined by comparator navigable map extensors
map and provides developers with possibility to return the closest matches for given such targets.
The most popular implementations of this interface are three map and concurrence key place map for multithreaded
environment.
We also have APSA class dictionary.
How it is different from the map interface you can reclass was created in Java version one and supposed
to be a parent class for all types that map keys to values.
Since multiple inheritance in Java is not supported by the map interface was introduced in Java version
two.
So I would say that dictionary is an old version of map interface and that is considered as an obsolete
and use of map is preferred.
But still, you might be asked about dictionary during the interview and for your common Java literacy,
you have to be aware about it.
Hash table is a concrete class that extends the dictionary type and internments map interface.
It was considered as a thread safe version of hash map unless concurrent hash map was introduced in
Java version five, the synchronization mechanism is not so efficient as it is implemented in concurrent
Hashmat, in case you are not familiar with the trending topic.
That would explain the difference in the implementation of low interest rate and support in these classes.
In simple words, hash table closes the door in your home.
Concurrent hash map closes a separate doors in your home to allow everyone to walk around the home until
only one room is closed.
Can you feel the difference in implementation of concurrency support in these two classes and meet later
in the separate lessons?
We are going to learn multithreaded together and we will get back to a thread safe map implementations.
That's how MAP Iraqis look like.
Before we proceed with the details, I want to finish the topic of differences between hash map and
hash table.
So the first thing that is different we already discussed on MassArt and hash table are synchronized.
And that's why hash table is considered to be a thread safe map.
Hash table also does not allow news, a new case nor new values.
On the other hand, hash map allows us to store one key and multiple values.
Hash table is a legacy class, whereas hash map is newer.
And now I suggest opening the source code of map interface to learn that the communication and methods
of this interface here is map interface.
Pay attention to this interface doesn't extend iterable interface like collection while review and map
interface.
I'm going to use and work by saying entry, I would always mean the key value pair.
Is it clear, for example, size returns, amount of entries in the map.
In other words, a number of key value pairs is empty.
This is self-described Macit.
It checks whether the current map is empty or no, contains key verifies whether a map contains specific
contains value, similar method to check value in map, yet method to get value from the map we have
to use.
So we ask you to get Masset to retrieve our value.
There is no method in map.
Instead, we have put method.
We can put key value pair in map.
That's why put method takes two arguments.
Pay attention to the return tab here.
Imagine that you put in map integer value one and some customer reference type user as a value to the
map in this case, but will return now and now.
Imagine that you put new value but with the same key you call put MassArt again was integer value one
and new user object.
In this case, both method will return the value that was previously associated with the key.
That means previous user.
Does it make sense?
If you want to remove some entry from the map, you have to call remove Masset and Paskey as a method
argument map interface, declare methods for bulk operations, for example.
Put all method can add all entries from one object to another.
Clear message.
Remove all mappings from the current map object.
He has said massive returns contain the offset type that contains only keys from this map object.
Why set?
Because all keys are unique and map.
That's why I said type is returned.
In case we want to retrieve all whaler's from the map, we have to call values method that returns collection
of elements.
Why collection?
Because values are not unique and collection is returned.
Here's one more very useful method and set this massive return set of entries of this map.
Each key value pair is represented as a single object of type entry.
You really understand why set returned?
Because each key value pair is unique and three is in our interface in map.
Let's look at it.
Entry type has to match assets that are used more often than others.
They are yet key and get value.
For example, we can iterate over a set of entries in foreach loop and in the foreach loop body we can
use reference to the entry object together is a key or value.
It depends on what exactly we need.
According to our logic.
There are also other methods that data on my opinion are not used very often.
For example, set value.
It allows us to set value associated with the current key in the entry.
In my opinion, if there would be such need, I mean to update the value associated with a specific
key, most likely you want to do this with entry time, but instead we'll just call good method with
the same key on map object to update the value.
But probably one day you would face with the case that while iterating over entries, you need to implement
logic that under some specific conditions you have to update value that is associated with a specific
key.
And for this case, you already know what method can help you with that.
You can see equals and hash code.
You already know these methods and also entry has a few interesting static methods.
We are going to use them in our next lessons.
Right now.
Let me explain you and see what I say about we have comparing biochem assets at source entries by case
in case there implement comparable interface and we compare the parameter in case keys don't implement
comparable interface.
These methods return compared to that we can use to search and now attention to search our entries set,
but not elements in map bytes key.
And we also have comparing by value method that returns comparisons that can compare entries set by
values.
Imagine that you want to search your map by.
Well, to be honest, compared to that source, my case is not so cool.
Why?
Because we can create object of three map and put all elements from any map there and elements will
be sorted automatically by case, but sorting by various.
That is really helpful compared to now when we answer it, what entry type is.
Let's investigate what matters are left here in the map interface equals and hash good methods and.
The default message I described, for example, here is interesting and useful message.
Imagine that you need to get away from that, but you don't want to get new in case Element is absent
in map.
Instead, you would like to get some default value we can call yet or default to retrieve element by
key in case and market for such key present or to return default value in case there is no mapping for
such key.
Pay attention to the fact that in case we exist and values that is associated with this key is no value,
then no will be returned.
Default value will be returned only in case there is no such key in the map.
For each matsutake takes function that should be applied for each and every one will return functional
programming with Java.
That will show you how you can apply this method.
Replace all text functions that will be applied to each entry and each entry in turn will be substituted
with the result that will be returned by this function.
But if absent method should be used when you don't want to override the values that is already associated
with a specific key.
So new value will be put into the map only in case there is no entry with such key or keys associated
with no value.
The removal method can help you to remove entry by specifying the key value pair here.
Also replace MassArt.
They are different from Puth method because you can say that you want to replace Element only with a
specific key and with a specific value, or in case of overall that method only in case such key exists.
Comparative absence method allows you to compute value in case he is absent.
We can pass the keys that doesn't exist in map and function that can compute new value.
In case such care exists, the value won't be updated.
Compute if present method takes key and functions that can compute the value based on the current key
and previous value that is associated with it in case he exists and previous value is not.
Now, the result of the function will be associated with this key compute method.
Attempts to compute and mapping for the specified key and its current mapped value is there is no current
mapping.
Mersch method works in the following way.
It is a specified key, is not already associated with a value or is associated with.
Now associate it with the given no new value.
Otherwise replace the associated value with the results of the given remapping function or removes if
the result is now lost for assets.
Require knowledge of functional programming in Java.
That's why they will be reviewed in detail separately in the lessons about functional programming.
Right now it is enough high level understanding what this message do, and now we are done with all
default methods in my interface and it is time to review static methods in this interface.
Here we see overloaded static method with the name of this method, say here since Java version nine
and they can create immutable.
That also means unverifiable map that contains a limited number of mappings.
And you can see that the only difference between all of these methods is a number of mappings that it
takes as a parameter.
Here's a method that takes variable lengths, argument of type entry, and based on these creates and
modifiable map and remasters returns and modifiable entry type based on the key value pair that is passed
to the method, why this method is needed.
This method is needed, for example, for the previous method that we have just discussed, because
you want to eliminate the case when your entries changed because somebody got access to it by reference.
That's why you might want to create an modifiable entry first.
And this method will help you in that.
And the last but not least method for today is copy of.
If we want to create the copy of the map that nobody will be able to modify, we have to call this method
and parse the map that we want to copy as an argument to this method.
That's all method for map interface.
I believe that right now, you know, map interface even better than some senior software engineers.
Let's recap what we have learned today.
Today, we learned Map Iraqi.
We talked about features that are introduced in the implementations of map interface.
We also talked about source, map and navigable map.
We discussed what dictionary is.
Now, you know what the difference is between Hash Table and Hashmat.
After that, we learned methods of map interface.
We also learned what entry type is.
That's all what we have for today.
Thanks a lot for your attention.
See you in the next lesson.