Skip to main content

Why do I need to override the equals and hashCode methods in Java?

 Imagine you have this

MyClass first = new MyClass("a","first");
MyClass second = new MyClass("a","second");

Override only equals

If only equals is overriden, then when you call myMap.put(first,someValue) first will hash to some bucket and when you call myMap.put(second,someOtherValue) it will hash to some other bucket (as they have a different hashCode). So, although they are equal, as they don't hash to the same bucket, the map can't realize it and both of them stay in the map.


Although it is not necessary to override equals() if we override hashCode(), let's see what would happen in this particular case where we know that two objects of MyClass are equal if their importantField is equal but we do not override equals().

Override only hashCode

If you only override hashCode then when you call myMap.put(first,someValue) it takes first, calculates its hashCode and stores it in a given bucket. Then when you call myMap.put(second,someOtherValue) it should replace first with second as per the Map Documentation because they are equal (according to the business requirement).

But the problem is that equals was not redefined, so when the map hashes second and iterates through the bucket looking if there is an object k such that second.equals(k) is true it won't find any as second.equals(first) will be false.

Comments

Popular posts from this blog

How to answer conflict-resolution interview questions

QUESTION 1: How do you deal with conflict? People aren’t going to get along with each other all the time. It’s just a fact. Employers want to know that you can respond to conflict diplomatically, says job-search and interview coach Thea Kelley. If you’re a my-way-or-the-highway type of personality, you’re not going to get very far in the interview. Start off by emphasizing communication and respectfulness as a means to conflict resolution. For example, “I always take the person aside and discuss the issue privately. I listen actively to make sure I understand the other person’s point of view, and I work with the person to develop a solution together.” Stress that even if you both don’t completely agree on the end result, you tried to at least meet each other halfway. Pro tip: “Don’t non verbally communicate resentment when telling a story,” Kelley advises. “Aggressive body language and tone of voice can show that you harbor bad feelings.” QUESTION 2: Tell me about a time when you had

Java Logger

In Java, logging is an important feature that helps developers to trace out the errors. Java is the programming language that comes with the logging approach. It provides a Logging API that was introduced in Java 1.4 version. It provides the ability to capture the log file. In this section, we are going to deep dive into the Java Logger API. Also, we will cover logging level, components, Logging handlers or appenders, logging formatters or layouts, Java Logger class, What is logging in Java? In Java, Logging is an API that provides the ability to trace out the errors of the applications. When an application generates the logging call, the Logger records the event in the LogRecord. After that, it sends to the corresponding handlers or appenders. Before sending it to the console or file, the appenders format that log record by using the formatter or layouts. Need for Logging It provides the complete tracing information of the application. It records the critical failure if any occur in