Java interview question on hashcode() and equals() || Java collection interview questions on HASHMAP

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys today in this tutorial we're going to talk about a very important interview question and this question is kind of based on the hashcode and equals matter so that question that I'm gonna discoursing over here this has been asked many times in many of the interviews just to check the candidates hashcode and equals matters understanding okay so you know I don't want to ask you the question right in the beginning because if I show you a load of core maybe you guys may be confused okay so what we're gonna do over here I'm gonna type in the core and in between I'm going to ask you two different questions okay and you need to guess the answer for that right so let's start scoring right now [Music] right now so I do have a very simple class called M over here and I do have a man matter okay so now let's start scoring over here all right so now let's assume that I do have another class here called employee okay and in this employee class I do have a non-static variable which is ID okay so each employee will have a ID all right so to initialize this ID in the time of object creation let's say I do have a very simple constructor okay so let me create the constructor over here let's say employee here and that's it and let's say is accepting your ID and this setting this ID do the non-static variable do this right okay so it's a very simple class so now let's say I want to create two employee object here okay so how can I create the object for employees so I can write employee here and let's say EMP one and let's say new employee off I'll pass in the ID okay so this employee has the ID this employee EMP one has the ID one okay now let's say I do have another employee with the same ID so if I do have another employee with the same ID that means both the employee is same okay this is the same person because is the employee will have a same ID okay so just pay your attention because the question is coming all right so the state over here now let's say I have EMP two and let's say the ID is again one okay so this one is the idea over here right okay so now I do have two different employee object with the same idea so now what I'm going to do I'm going to put these two employee reference to a hash map okay okay so check it out over here let's say I have a map here and my map lest there is except employee agent key and a value which is string okay and let's say SMAP equal to new hash map okay and there we go so now what I'm going to do let's say map dot put okay so the key will be a employee object right so I already have an employee object over here EMP one so as a key I'm going to put this EMP one object here which is an employee object and the value i'll let's say the value is jack okay and let's say i want to put the another employee object again over here so this is EMP two which has the same ID one so I'll put this EMP two object over here and the name again I'm I'm going to put Jack right so actually I'm putting the same employee information to time over here in this particular map so what you need to guess it over here let's say I have a I want to check the size of this particular map so if I'm writing here a map dot size you need to guess the size of this particular map right you need to guess it and and yes now just check it out okay you just need to guess the size of it okay so I assume that you have already guessed the output for this particular size of this particular map okay but if not don't worry because I really want you to understand what will be the output because because if I'll proceed further right now you will not understand if you can't guess the size of this particular map okay so to make you understand this I can make it a little more simpler I can give you a clue just think that there is a this is an integer class right so I do have integer I equal to new integer and this integer there is a value one over here and I do have another integer over here let's say I won and again I'm writing integer over here and let's say the value is again one right now again let's say I do have a hashmap okay let me copy and past it okay so this is a different question okay don't don't get mash up okay this is something really different okay I just want to ask you another map size question okay it's very simple let's see so here I'm taking integer here and let's say I have integer here as a key and string as a value and let's say this is a map one this is another map right this is another map over here and here again I'm putting a m map one dot put and I'm putting I here and the value let's say one okay and again let's say this is I 1 and the value is again 1 because this I value is 1 this I 1 value is 1 right ok so now again I am just going to print out this size of this map and you need to guess that ok so let's say map 1 dot size ok so now you just guess the size of this particular map okay so what will be the size of this map and what will be the size of this map this is really really important to understand before I go for my question ok so let's clear up our confusion over here ok so this particular map size will be 1 and this particular map size will be 2 ok so if I if I run it right now you can see the first map size is 2 and the second map size is 1 okay by seeing these two outputs we can be make sure that in the first map the duplicate elements are getting inserted isn't it this EMP one and EMP - both are pointing to the same jack object having those same employee ID which is one but still the map size is showing do isn't it but here we are having the same integer which is one what I and I one value is one as well but being the duplicate elements this particular map is allowing only one element to it so the size that we are getting over here is 1 ok so what you need to do is over here so here goes the question you need to if you talk about this particular map over here let me separate this this is a some sort of blue - you okay you can use this one as a clue so what do you need to do over here this particular map should return one as a size right so the duplicate element should not be enter to this particular map instead of two you should get the map sizes one ok so how you will do it ok so the reason why I have written this piece of core just to give you a clue why you are getting one as the output over here but why you are getting 2 as output over here and how you can make it one okay that's your question and that you need to think ok and this is a clue to you all right ok so now let me tell you the answer just think about the integer class so this integer is a wrapper class right and as you know all the wrapper classes I mean it may be integer it may be character it may be double or any wrapper classes has already implemented the hashcode and equals method into it I mean the hashcode and equals method is already been overridden do all the wrapper classes isn't it to make you sure the I'm inside a Java API right now this is the Java API but using this URL you can go to the API and this is the integer class right so if you talk about this integer class in Java then we can see there is a matter called equals which is right over here and already there is a method present here which is hash code so all the hashcode and equals method is already been operating to the wrapper class I mean any of the wrapper class it may be integer it may be double or it may be any other wrapper classes isn't it so what hashcode and equals method does I assume that you already know it if you don't I will make another video for that but to I mean to make it very short I can say that you know if do object hash code is same I mean at that time the equals method is going to be used right and there the contents will be checked and if the hash code is same and the content is same then that will be a duplicate element or that would be a duplicate object and this that should be rejected isn't it so if you talk about over here inside the employee employee class there is no high score in equals method present over here isn't it but inside the integer class there is already a high score and equals method present which is actually verifying the duplicates element and stopping to make an entry into this particular map isn't it so we need to do the same thing with our employee class we just need to generate the high score and equals method and you don't need to think about think much about how to generate the high score in equals method yes if you want you can write your own implementation but over here I'm going to use my IDE to generate the high score in equals method and to do that I'll go for the stores and I'm gonna click over here generate a score in equals right and I'm gonna click OK over here and there we go so this is my high score matter and this is my equal matter isn't it so now my employee class has put the high score and equals method all right so what I need to do right now I just don't need to do anything else I am just going to run this program again and here you can see the first map size is 1 and the second map size is 1 right so there we go so if you if you right now if you just you know if I'll just print the map dot entry here and if I run it you can see only one particular element is getting inserted to the particular map but if I will come in this particular out ok and if I will run this program you can see both the element Jack and Jack is getting inserted all right so this how the hash I mean the the high score and equals contracts work and this is a very very important interview question we shall be asked so many times in the interview and you know it's very important that you should have a good hole in it okay so this is it for today and I'll be covering some more question in near future thank you very much for watching and have a nice day [Music]
Info
Channel: Selenium Express
Views: 275,305
Rating: 4.6597724 out of 5
Keywords: java collection interview questions, hashcode and equals method in java, java collection interview questions and answers, hashcode in java, core java interview questions and answers for experienced, hashmap hashcode and equals in java, java interview questions and answers, java interview questions and answers for freshers, equals and hashcode in java, java object class methods, selenium express, java, hashcode, equals, java interview questions and answers for 3 years experience
Id: Nr56SlbMed4
Channel Id: undefined
Length: 13min 48sec (828 seconds)
Published: Sat Jan 20 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.