How can I overload equal method to make different objects have same hashcode value in unordered_multimap?

What is the need of overriding hashCode method when overriding equals method?

  • what will happen if we override the hashCode method? I do know that hashCode will be the same for two objects, that means there is only one object in the memory ??? what is the need for XOR operation done on variables of my class during returning hashCode method? PS: If you want any further clarification regarding my question i will add more details.

  • Answer:

    If you want people to do your homework for you, you could at least try to provide more detailed information. For example, the programming language you are concerned with.

praveen at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

If you want to override equals method in your class, you should also override hashCode method as well. public int hashCode() { final int PRIME = 31; int result = 1; result = PRIME * result + number; return result; } public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; final Scrap other = (Scrap) obj; if (number != other.number) return false; return true; }

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.