Java equal and hashCode with non primitive types

I have a class with some non-primitive members.

class Relation { String name; Role roleFrom; Role roleTo; } class Role { RoleType roleType; String details; } class RoleType { String typeName; String details; } 

Two ratios are equal when

  • name is equal
  • role type (identified by a unique type) is equal for role members (roleFrom and roleTo)

How to write equals and hashCode for Relation class. When you try to use Netbeans, it displays only 3 fields ( name , roleFrom and roleTo ). This is because you do not need to access primitive types in roleFrom and roleTo (roleType -> typeName). Or please show the implementation.

thanks.

+4
source share
4 answers

When implementing hashCode() and equals() with non-primitive field types, it is assumed that these types also correctly implement hashCode() and equals() . So go to other classes and implement hashCode() and equals() there (again using the autogeneration functions of your IDE).

+7
source

You should be especially careful when overriding your equals method, which is best done, as Joshua Bloch noted in Effective Java, so as not to override it at all if absolutely necessary, with the standard equal implementation inherited from java.lang.Object, each object is only equal to itself.

I recommend that you check the following links to find out how to implement both the equals and hashCode methods correctly. The last link shows you how to implement these methods when you have non-primitive types, as well as considerations for when you are dealing with inheritance.

+5
source

One potential solution, assuming you

  • you must redefine equals for your implementations
  • You want to be lazy
  • You do not mind treating hashcode () and equals () methods as black boxes
  • Do not pay attention to using an additional tool at compile time.

- try to use the Lombok useful EqualsAndHashcode Designation to handle this for you. Of course, probably, you probably should probably read the links that @StudiousJoseph mentioned in your answer, so you understand how to write the right equals / hashcode methods, but it is a tool that can help reduce some of the problems and mistakes when writing these methods yourself.

As mentioned above, in your case you need to at least use this annotation or override equals () / hashcode () in your relationship class and possibly also your own Role and Roletype class if you need to configure peer behavior on them.

Edit: I just noticed that you are using Netbeans. This may be relevant if you want to try Lombok: http://wiki.netbeans.org/Lombok#IDE_support

0
source

As others have said, you need to implement equals / hashCode for the other two classes. Where is the example:

 class Relation { String name; Role roleFrom; Role roleTo; public int hashCode() { // Just a sample. super.hashCode() would probably be better return name.hashCode() & roleFrom.hashCode() | roleTo.hashCode(); } public boolean equals(Object o) { if(!(o instanceof Relation)) return false; Relation other = (Relation)o; return name.equals(other.name) && roleFrom.equals(other.roleFrom) && roleTo.equals(other.roleTo); } } class Role { RoleType roleType; String details; public int hashCode() { // Just a sample. super.hashCode() would probably be better return roleType.hashCode(); } public boolean equals(Object o) { if(!(o instanceof Role)) return false; Role other = (Role)o; return roleType.equals(other.roleType); } } class RoleType { String typeName; String details; public int hashCode() { // Just a sample. super.hashCode() would probably be better return typeName.hashCode(); } public boolean equals(Object o) { if(!(o instanceof RoleType)) return false; RoleType other = (RoleType)o; return typeName.equals(other.typeName); } } 

As for if(!(o instanceof XYZWK)) return false; : this is basically just unsuccessful for the equals method, without throwing (or needing try / catch for) a ClassCastException .

-1
source

All Articles