aspose file tools*
The moose likes Java in General and the fly likes Overwrite hashCode with high performance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Overwrite hashCode with high performance" Watch "Overwrite hashCode with high performance" New topic
Author

Overwrite hashCode with high performance

Abhra Kar
Ranch Hand

Joined: May 22, 2008
Posts: 122
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.

Thanks in advance
Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1029
    
  15
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

Joined: Jul 26, 2007
Posts: 10451
    
    8

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


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Abhra Kar
Ranch Hand

Joined: May 22, 2008
Posts: 122
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

Joined: Jul 26, 2007
Posts: 10451
    
    8

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

Joined: Apr 06, 2010
Posts: 4422
    
    8

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

Joined: May 22, 2008
Posts: 122
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

Joined: Mar 14, 2011
Posts: 6
Hi

Please follow java source code. You can refer java.util.HashMap class for your problem.


Thanks
Asis
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
I don't think reading source code is going to help you.
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

Joined: Oct 02, 2003
Posts: 11411
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
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

Joined: Mar 17, 2011
Posts: 8008
    
  22

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Overwrite hashCode with high performance