-1

A few days ago I had an interview and there a few interesting questions occurred. Namely, the recruiter asked me meticulous questions. Some of them concerned the close relationship between HashCode and the collections, which are based on the above-mentioned mechanism.

The first question was more or less like: what impact does the quality of hashCode have on the work of hashMap? The question was not literally worded, but I think that the question was about performance.

The second question touched on the problem of modifying one of the value(s) of the hashMap pair(s) and the potential impact of this operation on performance and the problems that may arise from such a procedure. It wasn't explicitly stated how to modify the value, but I think it was a put() method. For instance, we have a pair in hashMap like ["Programming", 7]. What will happen if we modify the value 7 or "Programming".

The third question was open and sounded like this: How is the hashCode value calculated for an object without any fields.

For the second question I asked that:

we are not able to modify key of the pair because in the hashmap internal implementation key modifier is marked as final

and if we want to do it we should remove this particular pair and add it with desirable values.

I asked either that modifying the value of the pair (key-value) in hashMap has no impact on HashCode because we should base the calculation on the immutable keys like String but I was not able to realize if it was enough for the recruiter due to the fact that this interview was not face to face.

Honestly, this interview was a little bit confused because the recruiter was so enigmatic, that's why I would like to know if from your point of view you can see another bottom of this three questions and I could add some additional information in my response.

I would be grateful if you could suggest what could have been behind these enigmatic questions.

Martin
  • 1,139
  • 4
  • 23
  • 49
  • 6
    Honestly, your question is extremely confusing. Dont throw just a large text block at us. Identify the core question you have, and ideally, write a bit of code to explain. – GhostCat Aug 16 '19 at 13:47
  • 1
    "we modified the value of one particular pair in HashMap" irrelevant to the hash map. Only the key is considered. – Andy Turner Aug 16 '19 at 13:49
  • See [here](https://stackoverflow.com/questions/13114043/java-hashset-contains-duplicates-if-contained-element-is-modified/13114376#13114376), perhaps, for what he might've meant by hashCode inconsistency. After all, hashCode usually has nothing to do with the values of a HashMap. – Avi Aug 16 '19 at 13:49
  • I am confused either. The entire interview and questions were enigmatic and for instance when I asked ,,how we are modifying value'' he said something like ,,just modify it''. I think that he was thinking about the put() method but I am not even sure. – Martin Aug 16 '19 at 13:51
  • "we modified the value of one particular pair in HashMap" - what exactly do you mean by "pair"? – DodgyCodeException Aug 16 '19 at 13:56
  • @DodgyCodeException Pair in the sense of key-value. – Martin Aug 16 '19 at 13:57
  • In that case, it's ok to modify the value (as long as the key remains the same). You don't have to call `put()` to modify the value. You can "just modify it", just as your interviewer said. In other words, `map.get(key).setSomeField(newValue);` – DodgyCodeException Aug 16 '19 at 14:01
  • @GhostCat At the beggining, I was thinking that my question will be not clear but trust me, I was completely confused too for the entire interview where each question was cleared as mud. – Martin Aug 16 '19 at 14:02
  • 1
    @GhostCat Problem clarification added. I hope that now it's clearer than it was. Thanks for your commitment. – Martin Aug 17 '19 at 12:07
  • @Marvin I don't know if I should tag you, but I will. – Martin Aug 17 '19 at 12:10

2 Answers2

1

the interviewer asked why HashCode quality is so important in the situation when we have an object in HashMap and for some reason, we modified the value of one particular pair in HashMap.

Hash code quality is important to properly distribute keys in the HashMap. Imagine if all keys had the same code - we'd need a linear number of operations to find a key.

Regarding the modification question - say we have an object with a hash code 5. and we use it as a key in a HashMap to store some value. The HashMap palaces this the key in the bucket for 5.

We then modify the key without removing it, so its hash code is now 10. What do we get? A key with the hash code 10 in the bucket for hash code 5. This is bad, since now we won't be able to find it in the HashMap.

However if we remove the key, modify it, and only then put it back with the original value, then everything is where it should be.

how we can calculate hashCode for the completely empty object(without any fields)

Just use the default Object.hashCode() implementation for that object. Don't override hashCode()

Malt
  • 28,965
  • 9
  • 65
  • 105
1

Following is on the assumption that I understood the questions correctly:

Answer to question 1: is modification of values don't have any impact on the way HashMap key values are stored but if the key and values are not immutable then the changes will be reflected in the HashMap objects as well. You might not be able to find the object from the HashMap if you modify the key outside, which is a reason why Keys should be Immutable.

Answer to Question 2: If you don't override hashCode in the class then the default hashCode implementation provided by the JDK is based on memory location which will be passed so whether the object is empty or not. If you override hashCode then whatever your hashCode function does, the result will be what you write.

Rakesh
  • 4,004
  • 2
  • 19
  • 31