Shfaq vetëm
0:03
S… Speaker 1 (13- The Map Interface)
So as I told you in the last video,
0:05
S… Speaker 1 (13- The Map Interface)
in Java we have an interface called map.
0:08
S… Speaker 1 (13- The Map Interface)
This interface is declared in the java .util package and it's
0:12
S… Speaker 1 (13- The Map Interface)
part of the collections framework,
0:13
S… Speaker 1 (13- The Map Interface)
but it's not part of this hierarchy that you saw earlier.
0:15
S… Speaker 1 (13- The Map Interface)
So it's not a collection,
0:17
S… Speaker 1 (13- The Map Interface)
it's not an iterable,
0:18
S… Speaker 1 (13- The Map Interface)
it's something entirely different.
0:20
S… Speaker 1 (13- The Map Interface)
Now, as you can see over here,
0:21
S… Speaker 1 (13- The Map Interface)
this interface has two generic type parameters,
0:24
S… Speaker 1 (13- The Map Interface)
K and V,
0:25
S… Speaker 1 (13- The Map Interface)
which are short for key and value.
0:27
S… Speaker 1 (13- The Map Interface)
So with a map,
0:28
S… Speaker 1 (13- The Map Interface)
you're essentially mapping a key to a value.
0:30
S… Speaker 1 (13- The Map Interface)
This value could be an object like a customer,
0:33
S… Speaker 1 (13- The Map Interface)
and this key is something that we use to look up that object,
0:36
S… Speaker 1 (13- The Map Interface)
like look up that customer.
0:37
S… Speaker 1 (13- The Map Interface)
Let's say you want to look up customers by their email.
0:40
S… Speaker 1 (13- The Map Interface)
So we create a map of string to
0:44
S… Speaker 1 (13- The Map Interface)
customer.
0:45
S… Speaker 1 (13- The Map Interface)
So our keys are going to be strings and our values are going to be customer objects.
0:49
S… Speaker 1 (13- The Map Interface)
We call this map and set it to a new hash map.
0:53
S… Speaker 2 (13- The Map Interface)
Again,
0:54
S… Speaker 1 (13- The Map Interface)
this interface has many different implementations,
0:57
S… Speaker 1 (13- The Map Interface)
but the one that you use most of the time is the hash map class.
1:00
S… Speaker 1 (13- The Map Interface)
Now let me create a couple of customer objects over here.
1:03
S… Speaker 1 (13- The Map Interface)
So C1,
1:04
S… Speaker 1 (13- The Map Interface)
we set it to a new customer.
1:06
S… Speaker 1 (13- The Map Interface)
Here we should pass a name.
1:08
S… Speaker 1 (13- The Map Interface)
Let's say A.
1:10
S… Speaker 1 (13- The Map Interface)
And then an email,
1:11
S… Speaker 1 (13- The Map Interface)
let's say E1.
1:12
S… Speaker 1 (13- The Map Interface)
I'm going to duplicate this line.
1:14
S… Speaker 1 (13- The Map Interface)
Let's create the second customer call it B with
1:18
S… Speaker 1 (13- The Map Interface)
E2 as the email.
1:20
S… Speaker 1 (13- The Map Interface)
Now we want to store these customers in our hash map.
1:24
S… Speaker 1 (13- The Map Interface)
So we type map .put,
1:27
S… Speaker 1 (13- The Map Interface)
this method has two parameters,
1:28
S… Speaker 1 (13- The Map Interface)
a key which is a string and a value which is a customer.
1:32
S… Speaker 1 (13- The Map Interface)
So, here I'm going to pass c1 .getEmail.
1:35
S… Speaker 1 (13- The Map Interface)
I'm going to use the email of this customer as a key to look it up,
1:39
S… Speaker 1 (13- The Map Interface)
and then I pass the customer object itself.
1:42
S… Speaker 1 (13- The Map Interface)
Similarly,
1:43
S… Speaker 1 (13- The Map Interface)
we add the second customer.
1:45
S… Speaker 2 (13- The Map Interface)
Now,
1:48
S… Speaker 1 (13- The Map Interface)
to get a customer from this hash map,
1:50
S… Speaker 1 (13- The Map Interface)
we type map .get,
1:51
S… Speaker 1 (13- The Map Interface)
we pass the key,
1:53
S… Speaker 1 (13- The Map Interface)
let's say e1,
1:54
S… Speaker 1 (13- The Map Interface)
and this will return a customer object.
1:57
S… Speaker 2 (13- The Map Interface)
So,
1:58
S… Speaker 1 (13- The Map Interface)
if we print this,
1:59
S… Speaker 1 (13- The Map Interface)
what do you think we're going to get?
2:01
S… Speaker 1 (13- The Map Interface)
We're going to see the name of this customer.
2:03
S… Speaker 1 (13- The Map Interface)
So,
2:06
S… Speaker 1 (13- The Map Interface)
There you go.
2:07
S… Speaker 1 (13- The Map Interface)
So instead of using a loop to iterate over a list,
2:11
S… Speaker 1 (13- The Map Interface)
we call the get method of the hash map to get an object.
2:14
S… Speaker 1 (13- The Map Interface)
And this is extremely fast.
2:16
S… Speaker 1 (13- The Map Interface)
It doesn't matter how many objects we have in this hash map,
2:18
S… Speaker 1 (13- The Map Interface)
we could have 1 trillion objects.
2:20
S… Speaker 1 (13- The Map Interface)
The cost of finding a customer is constant and it would be very
2:24
S… Speaker 1 (13- The Map Interface)
very small.
2:25
S… Speaker 1 (13- The Map Interface)
Now what if we look for a customer that doesn't exist in this list?
2:29
S… Speaker 1 (13- The Map Interface)
Let's say e10.
2:30
S… Speaker 2 (13- The Map Interface)
We get null.
2:34
S… Speaker 1 (13- The Map Interface)
Here we have a similar method called get or default.
2:38
S… Speaker 1 (13- The Map Interface)
So we can pass the key and a default value if this value
2:42
S… Speaker 1 (13- The Map Interface)
we're looking for doesn't exist.
2:44
S… Speaker 1 (13- The Map Interface)
So in this case,
2:45
S… Speaker 1 (13- The Map Interface)
we can pass,
2:46
S… Speaker 1 (13- The Map Interface)
let's say,
2:47
S… Speaker 1 (13- The Map Interface)
an unknown customer.
2:48
S… Speaker 1 (13- The Map Interface)
So I'm going to create an unknown customer over here,
2:52
S… Speaker 1 (13- The Map Interface)
new customer,
2:53
S… Speaker 1 (13- The Map Interface)
the name is unknown and the email is nothing.
2:57
S… Speaker 1 (13- The Map Interface)
So I'm telling this map,
2:59
S… Speaker 1 (13- The Map Interface)
look for an object with the key E10 and if you cannot find it,
3:03
S… Speaker 1 (13- The Map Interface)
return
3:04
S… Speaker 1 (13- The Map Interface)
this unknown customer.
3:06
S… Speaker 1 (13- The Map Interface)
Take a look.
3:07
S… Speaker 1 (13- The Map Interface)
Now we get unknown.
3:11
S… Speaker 1 (13- The Map Interface)
Another useful method is contains key.
3:14
S… Speaker 1 (13- The Map Interface)
So here we can call contains key.
3:17
S… Speaker 1 (13- The Map Interface)
You want to see if you have a customer with this email,
3:21
S… Speaker 1 (13- The Map Interface)
e10.
3:21
S… Speaker 1 (13- The Map Interface)
Obviously this returns a boolean,
3:24
S… Speaker 1 (13- The Map Interface)
so let's rename this to exists.
3:26
S… Speaker 1 (13- The Map Interface)
Now we run the program.
3:28
S… Speaker 1 (13- The Map Interface)
We get false.
3:31
S… Speaker 1 (13- The Map Interface)
We also have a method for replacing an object in a hash table.
3:34
S… Speaker 1 (13- The Map Interface)
For example,
3:36
S… Speaker 1 (13- The Map Interface)
We can call replace,
3:37
S… Speaker 1 (13- The Map Interface)
and we want to replace this customer with the email E1
3:42
S… Speaker 1 (13- The Map Interface)
with another customer object.
3:44
S… Speaker 1 (13- The Map Interface)
Let's say we made a mistake,
3:46
S… Speaker 1 (13- The Map Interface)
and this customer object,
3:48
S… Speaker 1 (13- The Map Interface)
its name should be A++ with the same email.
3:53
S… Speaker 2 (13- The Map Interface)
Now,
3:54
S… Speaker 1 (13- The Map Interface)
this method is going to replace the customer,
3:57
S… Speaker 1 (13- The Map Interface)
so if we print our entire map,
4:04
S… Speaker 1 (13- The Map Interface)
You can see that this map has two key value pairs.
4:07
S… Speaker 1 (13- The Map Interface)
Here's the first key value pair and here's the second key value pair.
4:11
S… Speaker 1 (13- The Map Interface)
So this is the reason this interface is called map in Java because
4:15
S… Speaker 1 (13- The Map Interface)
it maps a key to a value.
4:17
S… Speaker 1 (13- The Map Interface)
in C sharp and python is called a dictionary because it's kind of like a dictionary
4:22
S… Speaker 1 (13- The Map Interface)
where we have words and their meanings.
4:24
S… Speaker 1 (13- The Map Interface)
So each piece of data has two components,
4:26
S… Speaker 1 (13- The Map Interface)
a key and a value.
4:27
S… Speaker 1 (13- The Map Interface)
Okay?
4:28
S… Speaker 1 (13- The Map Interface)
Now,
4:29
S… Speaker 1 (13- The Map Interface)
as I told you before,
4:30
S… Speaker 1 (13- The Map Interface)
this map interface is not an iterable object,
4:33
S… Speaker 1 (13- The Map Interface)
so we cannot use it in a for each loop.
4:35
S… Speaker 1 (13- The Map Interface)
So if you write for each item in map,
4:38
S… Speaker 1 (13- The Map Interface)
look,
4:39
S… Speaker 1 (13- The Map Interface)
we get a compilation error because this is not iterable.
4:43
S… Speaker 1 (13- The Map Interface)
So how can we find the objects we have stored in a map?
4:46
S… Speaker 1 (13- The Map Interface)
Well, here we have three methods.
4:49
S… Speaker 1 (13- The Map Interface)
One of them is key set.
4:51
S… Speaker 1 (13- The Map Interface)
This returns the list of keys in this map.
4:53
S… Speaker 1 (13- The Map Interface)
This object is iterable so we can iterate over it.
4:56
S… Speaker 1 (13- The Map Interface)
In each iteration we get a key.
4:58
S… Speaker 1 (13- The Map Interface)
So we can print it over here.
5:00
S… Speaker 2 (13- The Map Interface)
There you go.
5:04
S… Speaker 1 (13- The Map Interface)
These are our keys.
5:04
S… Speaker 1 (13- The Map Interface)
We have a similar method called entry set.
5:09
S… Speaker 1 (13- The Map Interface)
Now look at the return type of this method.
5:11
S… Speaker 1 (13- The Map Interface)
It returns a set of entry of string
5:15
S… Speaker 1 (13- The Map Interface)
and customer.
5:16
S… Speaker 1 (13- The Map Interface)
So it returns a set and because sets are iterable,
5:19
S… Speaker 1 (13- The Map Interface)
we can iterate over them using a forage loop.
5:22
S… Speaker 1 (13- The Map Interface)
So in each iteration we get an entry,
5:27
S… Speaker 1 (13- The Map Interface)
this entry if we print it,
5:29
S… Speaker 2 (13- The Map Interface)
we see something
5:34
S… Speaker 1 (13- The Map Interface)
like this, key equals value.
5:35
S… Speaker 1 (13- The Map Interface)
Now we can also get the value by
5:39
S… Speaker 1 (13- The Map Interface)
calling the get value method or the key.
5:43
S… Speaker 1 (13- The Map Interface)
So if you need to access both the key and the value in each iteration,
5:46
S… Speaker 1 (13- The Map Interface)
you should iterate over the entry set.
5:49
S… Speaker 1 (13- The Map Interface)
And finally we have another method called values.
5:54
S… Speaker 1 (13- The Map Interface)
It returns a collection of customers,
5:56
S… Speaker 1 (13- The Map Interface)
so this is also iterable.
5:58
S… Speaker 1 (13- The Map Interface)
Let's rename this variable to customer.
6:01
S… Speaker 1 (13- The Map Interface)
So in each iteration we get one customer.
6:05
S… Speaker 2 (13- The Map Interface)
So A and B.
6:09
S… Speaker 1 (13- The Map Interface)
And by the way,
6:10
S… Speaker 1 (13- The Map Interface)
the order of these customers is not guaranteed exactly like our sets.
6:14
S… Speaker 1 (13- The Map Interface)
So just because we added customer 1 before customer 2,
6:17
S… Speaker 1 (13- The Map Interface)
doesn't mean when we print these customers,
6:19
S… Speaker 1 (13- The Map Interface)
they're going to come out in the same order.
6:20
S… Speaker 1 (13- The Map Interface)
So this is all about the map interface in Java.

Kjo transkriptë është krijuar nga AI (njohje automatike e fjalimit). Mund të përmbajë gabime - verifiko me audion origjinale për përdorim kritik. Politika e AI

❤️ Love STT.ai? Tell your friends!
Përmbledhja
Kliko Përmbledhja për të gjeneruar një përmbledhje të AI të kësaj transkripte.
Përmbledhja...
Pyet AI rreth kësaj transkripte
Pyet gjithçka rreth kësaj transkripte — AI do të gjejë pjesët e duhura dhe do të përgjigjet.