Java Essentials - Hash table in java
3K views
Oct 24, 2024
Java Essentials - Hash table 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
Hach table in Java
0:03
So this is the topic in this video. In because of hash table, we'll be storing a key value pair
0:10
That means depending upon the current content of the key, we can search for a certain value
0:16
So that is the key value pair and that is the main feature in this hash table
0:20
So let us go for more further discussion. So the hash table in Java collection
0:27
The hash table is a collection that can store the key value pairs
0:32
We will choose an object as a key and another object as a value
0:38
And then the key is hashed and the resulting hash code is used as an index at which
0:44
the item will be stored there. We can get the set of keys present in the hash table by using the method, that is a key set
0:53
method using this key set method we can get all the set of keys present in the hash table
0:58
and using the method get so this is a get method we can retrieve values from a given key so when
1:06
the given key will be there when key will be given then using this get method we can retrieve its respective value stored as a key value pair So for the better clarity let us go for one practical example and using the Java code
1:20
will be discussing this hash table into more details. Let us discuss some examples on these hash table and hash table objects
1:30
Here we have defined one hash table which will hold the string as string is a string object
1:35
as the key and integer object as the value. so we know that hash table contents key value pair so the integer class wraps the
1:45
value of the primitive type in an object so there is an integer and here we're
1:50
having the string class objects will be there and this is our hash table this
1:55
class implements a hash table which maps keys to values any non null object
2:01
can be used as a key or as a value here so here we have defined one hash table
2:08
class object that is a hash and it has got instantiated with the constructor so with this particular hash
2:15
we are going to add some key value pairs here so the respective method is put
2:19
so here string is the name is the key and here the respective integer it will be the respective value here so there a marks we actually having so ion 65 depok 80 joy 55 torun 75 and Rahul that is we having
2:38
here 70 here so now we are printing this st.h.h. here so let me see that
2:44
how the outcomes are coming so the student hash table is having the content that is
2:49
that to run 75 you see we added torun at the fourth place and then Joy 55 we
2:54
added joy at the third place depok 80 we did this one at the second place
2:59
Raul 70 we did that one adding putting this one at the last place and the
3:03
on at the first place but you see they the order in which they got added to this
3:10
ST hash that is our hash table object the order has not been meant meant
3:14
but the key value pair the respective key value pairs that that pair has got
3:19
maintained throughout so in this way you have got this one now let us suppose
3:23
I want to add another one say I'm going for the ST hash
3:30
dot put say if I give a same name say if I go for say joy if I go for say joy
3:39
with the value say say seven so initially you see the joy is having the value 55 now it has become 67 then what will happen as a key a key is repeating the same key so it
3:51
will it will be not a new entry there it will not put a new values key value
3:56
pair there so only the joy its value is marks 55 will be update with the 67
4:03
so 55 will get updated with the value 67 so let us go for that you can find
4:08
that with this joy we are having the value 67 here okay so key must be unique and that's why it is known as key and
4:16
value may repeat and with it with each and every key there should be some one not null
4:21
value system dot out of print alien the student hash table and the ST hash we got printed
4:26
you see we got printed this one now we're doing something different here so string
4:32
max key is equal to null so we are going to hold the name of that student who has secured
4:38
the maximum marks so I and T max marks is equal to zero and max key that is the
4:43
which will be holding the name is having that null string initialized at first
4:47
so for string key colon sth has dot key set so in case of st.hats has dot key set
4:53
all the key values will be will be there and for all the keys in the key set
4:58
the four
#Computers & Electronics
#Java (Programming Language)
#Programming