I have observed that in many interviews this question is asked - How will you create your own HashMap class in Java? last time I was asked this question in Globant interview.
I always wonder & want to say that why do I need to create my HashMap if I am using Java.
Rather this question should be - Do you know the working of HashMap in Java? - This right question was asked to me in Globallogic interview.
So one can get lot of pages on this working on the internet, so not going in that.
But will tell that working, also will explain the scenario when its performance will get hit, like if all the keys are returning the same hashcode, causing a long linked list & again the searching in that will be O(n) & not O(1).
So hashcode() must be efficient enough to return unique hashcode for different keys. And need to look thing when using custom objects as keys. Also in hashcode() you can look to use hashcode() of String class if you have String properties in your objects. Also take care of the immutability of such objects.
Also tell the improvements done in new Java versions i.e. since jdk1.8 where it is also using balanced binary tree for certain threshold.
Can discuss about the concurrency in your hashmap.
Can tell about why ordering in HashMap is not decided, because this ordering is dependent on the memory location of the keys & that location decided at the runtime depending on the address of the available memory to store that key. But once HashMap is populated & you are not making any change its data, then every time you iterate through this HashMap, you will get the elements in the same order everytime.
In newer versions of Java, check for the version mabe it is 9+, this ordering will not be present.
And during the interview with Globant on this, I used method set() instead of put() in actual HashMap class, then I was questioned that when put() is used in actual class then why I have used set(), & this is more like to receive one parameter & we need to send inputs i.e. key & its value.
I was wondering on this question that -
1) Why I am creating HashMap class when such class is in Java?
2) set() takes one input only, it is not a rule, it is just a convention being followed during coding
in general.
3) Its my class, I can give any relevant names to methods here, why you are comparing with
any original class.
I always wonder & want to say that why do I need to create my HashMap if I am using Java.
Rather this question should be - Do you know the working of HashMap in Java? - This right question was asked to me in Globallogic interview.
So one can get lot of pages on this working on the internet, so not going in that.
But will tell that working, also will explain the scenario when its performance will get hit, like if all the keys are returning the same hashcode, causing a long linked list & again the searching in that will be O(n) & not O(1).
So hashcode() must be efficient enough to return unique hashcode for different keys. And need to look thing when using custom objects as keys. Also in hashcode() you can look to use hashcode() of String class if you have String properties in your objects. Also take care of the immutability of such objects.
Also tell the improvements done in new Java versions i.e. since jdk1.8 where it is also using balanced binary tree for certain threshold.
Can discuss about the concurrency in your hashmap.
Can tell about why ordering in HashMap is not decided, because this ordering is dependent on the memory location of the keys & that location decided at the runtime depending on the address of the available memory to store that key. But once HashMap is populated & you are not making any change its data, then every time you iterate through this HashMap, you will get the elements in the same order everytime.
In newer versions of Java, check for the version mabe it is 9+, this ordering will not be present.
And during the interview with Globant on this, I used method set() instead of put() in actual HashMap class, then I was questioned that when put() is used in actual class then why I have used set(), & this is more like to receive one parameter & we need to send inputs i.e. key & its value.
I was wondering on this question that -
1) Why I am creating HashMap class when such class is in Java?
2) set() takes one input only, it is not a rule, it is just a convention being followed during coding
in general.
3) Its my class, I can give any relevant names to methods here, why you are comparing with
any original class.