Java Essentials - Hash map in java
5K views
Oct 24, 2024
Java Essentials - Hash map in java Watch more Videos at https://www.tutorialspoint.com/videotutorials/index.htm Lecture By: Mr. Arnab Chakraborty, Tutorials Point India Private Limited
View Video Transcript
0:00
HashMap in Java
0:03
What is HashMap? HashMap uses hash table and implements the map interface
0:10
And there are some basic differences between this hash table and also the hash map
0:14
So let us go for some further discussion on it. The HashMap in Java collection
0:20
The HashMap class uses a hash table to implement the map interface
0:26
And this allows the execution time of basic operations, such as your gates put to remain constant even for large data sets
0:37
And that is one of the advantages. And also, it also takes key value paired like our hash table and to store data into the map
0:47
So that means in our previous discussion, we have discussed that hash table stores the value
0:51
in the form of key value pair. And same thing will be followed here in case of hash map
0:56
and to store data onto the map in this case. The main differences between the hash table and hash map are, so point number one
1:06
hash map is not thread safe and not synchronized, but hash tables are threat safe
1:12
So that's why for the multi-threated environment, it is our suggestion to use these hash tables
1:18
Next one is that hash map can accept one null value, but hash table does not support null values
1:25
So that is another difference between this hash map and hash table So let us go for some practical demonstration to show you that how this hash map can be implemented can be used in our Java code
1:38
So here is the demonstration for you. In this program, we are discussing hash map and hash map objects
1:46
Now, what is the hash map? So here you can find that hash table based implementation of the map interface
1:53
And this implementation provides all of the option. map operations and permits null values and the null key the hash map class is
2:04
roughly equivalent to the hash table except that it is unsynchronized and permits
2:09
null but in case of hash table we didn't allow to insert any null key or the null
2:16
values but here in case of hash map the null values and the null keys are permitted
2:22
this class makes no guarantee as to the order of the map in
2:27
particular it does not guarantee that the order will remain constant constant
2:31
over the time also so let us go for one hash map object the name of the object
2:36
is employee hash it has got instantiated it will be holding this integer
2:41
wrapper class object as the key and string object as the value here so how to
2:46
add item to this hash map object the method is put so here the key is 12 and
2:52
the value is programmer key is 14 values instructor in this way we have added
2:57
five such values there so if we print this employee hash then what will be the outcome you can easily find that so the employee hash map will be having the value that is 18 tester 21 is equal to casual 12 is equal to programmer 13 is equal to programmer and 14 is
3:15
equal to instructed you see the order in which I added I put all these items
3:21
so they are not occurring in the same order and also you can find that the
3:26
value has got repeated but the key cannot get repeated here you see the value has got repeated so here we are having
3:33
this programmer here also are having this programmer but the key values are
3:36
different here so if you go for another one say if I put this one say for this
3:44
12 for this 21 rather I'm putting this one as debugger
3:55
so you can find that this 21 has got updated with the text debugger here so let us let us go so it is
4:08
my debugger so I'm changing the spelling here actually so that you see 21 is
4:14
now debugger so what is happening as the key values key are the same so that's
4:19
why the debugger is not a new item for this employee hash but this 21 the
4:25
respective value casual has got replaced and updated by the value debugger here so if it is this line and if you go on executing we can find that against this by 21 key the value is casual now see we going to search how to how to
4:44
search a particular item against the key so if employee has dot contents key
4:51
18 so you see 18 is present so that's why the true will be the outcome you can see the
4:58
contents key is returns bullion contents key returns bullion and it is under the
5:04
package Java dot util under the class hash map contents key is the respective
5:10
method name which will take one object as input argument object key as input
5:14
argument here we have passed this integer class wrapper class object 18 as input argument and it returns bullion to check whether the value the key
5:23
has been found there or not so in case of 18 the type of the employee
5:28
there is a system dot out of print talent the type of the employee will be employee has got
5:33
get 18 you can easily find that it is printing tester because against 18 we're
5:38
having the value tester here so tested is getting printed but what about this
5:41
108 so 108 is this respective key is not present here so it will go on
5:47
printing the key is not found so in this way in this particular demonstration
5:52
we have discussed into details with the demonstration and the sample programming
5:56
code hash map its concept and how to define objects and how to define and how to access
6:03
different methods from this object as well thanks for watching this video
#Java (Programming Language)
#Programming