Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Overwrite hashCode with high performance

Abhra Kar
Ranch Hand
Posts: 153
Hi,
I have a class called PhoneNumber with 3 property zone,state,phNUm.Two zone can contain same numbers. Like zone CA has 10k phone numbers[214324,214325,....] , zone MH has 10k numbers[214324,214325,...]
How to overwrite a hashCode() and equals() method with high performance to check the equality between two numbers.

Chan Ag
Rancher
Posts: 1089
14
Text in bold font means you're yelling. And yelling can really annoy the Moose. ;-)

So first turn off that bold font.

And then we can work on that problem statement.

Maneesh Godbole
Saloon Keeper
Posts: 11257
16
Abhra,

Chan is a Bartender and one of the things which a Bartender does around here, is make sure the posts are complying with the standards. To understand more, why posting in bold font is a bad idea, please read http://www.coderanch.com/how-to/java/KeepItDown

Can you tell us what is the logic between two numbers being equal? Does the zone enter the picture? e.g. CA 123 and MH 123. Would they be equal or unequal? Also who do you wish to take the hash code approach? Some background details would help us understand what problem you are trying to solve here

Abhra Kar
Ranch Hand
Posts: 153
Hi Maneesh,
I will follow the instruction in near future, I was just little upset with some words .All right Let's come to the topic.
a>CA 123 and MH 123 are treated as unequal.
b>I want to go through hashCode() approach to distribute the business logic between hashCode() and equals() .If we simply return 123 from hashCode() then both 10k similar numbers from each zone will have same hasCode[like for CA and MH hascode is 123].
Then we will have to write full business logic regarding object's equality within equals() method. hashCode() is not doing any great job in this case .I want both hashCode() and equals() methods take equal responsibility to decide object equality in this scenario.

Thanks and Regards,
Abhra

Maneesh Godbole
Saloon Keeper
Posts: 11257
16
So why can't you concat the zone and the number (MH123) and treat it as a String? Once you have a string, it becomes a simple string comparison using the string1.equals(string2) method.

Matthew Brown
Bartender
Posts: 4568
9
Abhra Kar wrote:;I want to go through hashCode() approach to distribute the business logic between hashCode() and equals() .If we simply return 123 from hashCode() then both 10k similar numbers from each zone will have same hasCode[like for CA and MH hascode is 123].
Then we will have to write full business logic regarding object's equality within equals() method. hashCode() is not doing any great job in this case .I want both hashCode() and equals() methods take equal responsibility to decide object equality in this scenario.

Well, the equals() method really should have the full logic for object equality, in my opinion. But you're right that just using the number isn't a great hashCode(), because for good performance in hash-based collections you want to minimise duplication of hash codes. A typical implementation - the sort of thing that an IDE would generate for you, would look something like this:

Abhra Kar
Ranch Hand
Posts: 153
Thanks ,
I have got some idea for this problem. Can you guys give me some links about how to write proper hashcodes.

Thanks and Regards.

Asisranjan Nayak
Greenhorn
Posts: 6
Hi

Thanks
Asis

Campbell Ritchie
Sheriff
Posts: 50699
83
• 1
Try: Horstmann and Cornell, Core Java II, vol I.
Try: Joshua Bloch Effective Java, chapter 3.
Try: Google for Angelika Langer Java equals hashcode

fred rosenberger
lowercase baba
Bartender
Posts: 12231
36
generally speaking, "high performance" isn't very precise. The advice you would get in the performance forum would be to better define what is acceptable performance and what is not - down to exactly how long the code can take to run. Otherwise, you are tilting at windmills. You will never be done, because it can always be made better or faster.

The other issue is readability. Often performance and readability are at odds with each other. If you tweak your code and save a microsecond per run, but you spend an extra 30 hours coding and debugging it, is it really worth it? Is a user going to notice saving that millionth of a second? probably not, unless they are waiting for several hundred million operations. And to make up for the lost 30 hours of your time...you'd need a LOT of operations.

I'm not saying you shouldn't optimize, or that it shouldn't be considered - but often, worrying about optimization first is the wrong approach. Get the code RIGHT and READABLE first, and then - if there is a documented reason/need - worry about optimizing it.

Mike Simmons
Ranch Hand
Posts: 3090
14
• 1
I recommend Objects.hash(Object... values) in java.util.Objects. This is more than fast enough for most applications, and highly readable. If you need better performance, then hand-writing it out like Matthew shows is good; I'd probably just streamline it to:

This assumes that number and state are never null.

Winston Gutkowski
Bartender
Posts: 10527
64
fred rosenberger wrote:generally speaking, "high performance" isn't very precise...

@Abhra: And that is very good advice. Whenever someone says "I need this to run fast", your first question should be: HOW fast?

However, there is one thing you can do that will allow you to easily implement fast hashcodes - Make your objects immutable (look it up).

If an object can't be changed, then its hashcode can be calculated when it's created; or alternatively, it can be cached. Either way, the hash calculation only needs to be done once - and how much faster can you get than that?

And personally, I'd say that a PhoneNumber class is a prime candidate for immutability.

Winston