• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Junilu Lacar
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst
  • Peter Rooke
  • Mikalai Zaikin
Bartenders:
  • Himai Minh

Properly overriding equals and hashCode

 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all ,

I have been seeing some stuff on how important it is to properly override the equals and hashCode methods and how most of the time people do it wrong. I want to make sure I am doing it right, right meaning conforming to the general contract of the equals and hashCode methods.

I have two classes Item and ShoppingCart. Item is considered equal if the description and cost are the same and Shopping Cart is considered equal both carts contain the exact same items. Here are my overriden equals and hashCode methods. Let me know if they look ok or that they need some fixing?

Thanks,

AMD

//Item equals and hashCode


//ShoppingCart methods almost the same as above
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If items is a List, you can use equals. See the definition of List's equals method:


What is cost's type? It looks like it may be float or double. Realize that storing currency in a floating point type is always asking for trouble because of rounding. Consider storing it in an integer type (cents or tenths of cents, etc...) or using BigDecimal or your own custom class. Back to float/double: do you know they have one last trick up there sleeve? floating point numbers can take on the value NaN (not a number) which doesn't obey the usual rules of the relational operators, for example, Double.Nan == Double.NaN is false! Of course, your cost probably never takes on that value, but the paranoid code for that in equals/hasCode.
[ February 03, 2006: Message edited by: Jeff Albertson ]
 
Andrew Mcmurray
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jeff,

Other then that does it look ok?

Thanks,

AMD
 
Marshal
Posts: 27667
89
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think your ShoppingCart's hashCode() method returns different values for two objects whose items are the same overall but in different orders. But then its equals() method returns false for two such lists, so technically that is okay.
 
If you live in a cold climate and on the grid, incandescent light can use less energy than LED. Tiny ad:
Master Gardener Program
https://coderanch.com/t/771761/Master-Gardener-Program
reply
    Bookmark Topic Watch Topic
  • New Topic