Best way to override equals, hashCode and toString in java

Every developer has to write some concrete class, using which certain business logic is achieved.  Since every class is a child of ‘Object’ class, it is always advisable to override equals, hasCode and toString methods. When overriding these methods, add @override annotation to indicate the java compiler, that this method overrides the corresponding method in super class.

Override equals()

Methods signature : public boolean equals(Object o) – equals method checks whether the passed object is equal to the method on which equals method is invoked. The default implementation in Object class compares object passed in argument and the Object on which equals method is invoked using == operator. If they are equal, it returns true.  Following approach has to be  used while overriding equals method.

  1. Check if the object passed in argument to equals method is not-null. If it is null, return false.
  2. Check if the object passed in argument and the object on which equals is invoked, are of same class using instanceof operator. If they don’t belong to same class, return false. Objects with no common ancestor cannot be considered equal.
  3. Cast the object passed in argument to the class of object on which equals method is invoked. This is guaranteed to succeed, as instance check was done before this step. After casting, check the equality of important primitive variables using == operator. If the class has reference variables to other objects, invoke equals methods on the object recursively to which the reference variable refers. Returns true, if all these conditions return true.

Check whether the your equals methods adheres to conditions specified in java doc.

Override hashCode()

Methods signature : public int hashCode(Object o)  – hashCode method returns an integer corresponding to the object. Default implementation of hashCode in Object converts object’s internal address to integer. When equals method is overridden, it is also important to override hashCode method. This is required to satisfy “If two objects are equal by overridden equals method, then their hash codes has to be equal”. It is not required that the hash codes has to be different for objects which are not equal.

Using HashCode builder provided by Apache commons relieves us from the internal details for building good distributed hash code. You can create new default HashCodeBuilder with hard-coded values or with chosen random values. Using add(*) methods for all primitives, the primitive values can be used to build hash code. Composite object can be added to hash code builder using add(Object o) methods. If the class is inherited from other class, add super class’s hash code using appendSuper method.

Reference : HashCode Builder java doc

Override toString()

Methods signature : public String toString(Object o) – toString method returns the string representation of object. The returned string has to be human-readable and gives the concise information about the state of the object.  The default implementation of object class print class name followed by @ sign followed by hexadecimal representation of hash code of corresponding object.  Using string concatenation is a bad idea for implementing toString as it creates unnecessary objects. StringBuilder can also be used to implement toString().

Again, using ToStringBuilder provided by Apache Commons relieves from building String by ourselves.  You can create default ToStringBuillder by passing current instance which can be used to build string representation. The style in which the information to be printed can also be changed by providing overridden constructor. Optionally, StringBuffer can also passed to be used used for building string. All the primitives attributes are added using add<primitive type>(“name of field”, <value of field>) method. The attributes in super class can also be added using addSuper method. The composite object can be added using addToString method.

Reference :  ToStringBuilder java doc

cssh

In this post, I will introduce you to cluster administration tool called clusterSSH (cssh)

cssh is an abbreviation for clusterSSH.  Normally ssh connection to a host is established by supplying  host name.  What if, you have to log in to many machines simultaneously. Definitely you could do one by one, but that is not the programmer way of doing things.  cssh  is an open source tool written in perl on top of ssh to log in to multiple machines simultaneously. cssh is available for linux, mac and solaris operating systems. cssh establishes individual ssh connection to all the machines and opens individual X-term window for each machine and it also opens an administrative console. The administration console can be used to send commands to all machines to which connection is established. Commands can also be run on individual machines by selecting a particular Xterm window.  cssh can be useful in various cases. It can be used to send the same command to all machines in a cluster at the same time. It can be used for debugging by tailing logs of all machines under the load balancer.

Open source page : http://sourceforge.net/projects/clusterssh/

Installation :

Ubuntu
 sudo apt-get install clusterssh (through apt)

Mac : cssh is available as csshx (https://code.google.com/p/csshx/)

( or)

  • Download the later .gz file
  • gunzip <filename.tar.gz>
  • tar -xvf filename.tar

you will find cssh excecutable inside the folder

Usage and examples :

cssh to 4 machines will look like

cssh-example

 

cssh <list of hostnames> eg.., cssh 1@aa.com 2@bb.com 3@cc.com

Custom tags can be defined in  /etc/clusters  in  format :

<tag> [user@]<server> [user@]<server> […]

eg :  remote 1@11.com 2@22.com

cssh remote

 will expand to

cssh 1@11.com 2@22.com

The options can be overridden in  /etc/csshrc or $HOME/.csshrc file.

Thanks,

Kirubs.