Difference hashcode and equal, JAVA Programming

 

The hashCode() and equals() methods have to be very important, when objects implementing these two functions are added to collections. If not implemented or implemented incorrectly at all then your objects added in a collection like a Set, Map or List can behave strangely and also is hard to debug.

 

Equals ():

This method performs if some other object passed to it as an argument is same the object in which this method is called. It is easy to implement the equals() method incorrectly, if you do not understand the design. The contract can be stated in terms of 6 easy principles as given:

 

1. l1.equals(l1)  which defines an Object could be equal to itself.

 

2. l1.equals(l2) if and only if l2.equals(l1) So it may be incorrect to have your own class define "MySet" to have a equals() function that has a comparison with an Object of class "java.lang.String" class or with any other in built Java class.

 

3. l1.equals(l2) && l2.equals(l3) implies that l1.equals(o3) as well It defines that if the first object l1 same to the second object l2 and the second object l2 is similar to the third object l3 then the first object l1 is similar to the third object l3.  

 

4. l1.equals(l2) gives the same as long as l1 and l2 are unmodified if two objects are same, they must remain equal as long as they are not changed. Similarly, if they are not same, they must remain non-equal as long as they are not changed.

 

5. l1.equals(null)  which defines that any instantiable object is not same to null. So if you pass a null as an argument to your object l1, then it could give false.

 

6. l1.equals(l2) implies l1.hashCode() == o2.hashCode() ?? That is very important. If you define a equals() function then you must describe a hashCode() method as well. Also it gives that if you have two objects that are same then they must have the same hashCode, however the opposite is not true. So, If a field is not used in equals(), then it cannot be used in hashCode() function.

 

hashCode():

 

This method gives a hashCode() value as an Integer and is supported for the benefit of hashing based java.util.Collection classes like HashMap, Hashtable, HashSet etc. If a class overrides the equals() method, it must define the hashCode() method as well. The general design of the hashCode() method is that:

 

1. Whenever hashCode() method is called on the similar types object more than once during an execution of a Java program, this method must consistently give the same integer result. The integer result has not remained consistent from one execution of the program to the next execution of the same program.

 

2. If two objects are same as per the equals() method, then using the hashCode() method in each of the two objects must give the similar integer result. So, If a field is not used in equals(), then it cannot be used in hashCode() method.

3. If two objects are non equal as per the equals() method, each of the two objects can give either two same integer results or different integer results.

 

Posted Date: 7/25/2012 1:56:55 AM | Location : United States







Related Discussions:- Difference hashcode and equal, Assignment Help, Ask Question on Difference hashcode and equal, Get Answer, Expert's Help, Difference hashcode and equal Discussions

Write discussion on Difference hashcode and equal
Your posts are moderated
Related Questions
A joinpoint is a point in the implementation of the application where an aspect can be plugged in. This point could be a method being called, an exception being thrown, or even a f

Each time an object is operated in Java, it goes into the area of memory named as heap. The Java heap is named the garbage collectable heap. The garbage collection may not be force

What are the role of Keywords in java? Keywords are identifiers such as public, static and class in which have a special meaning inside Java source code and outside of comments

JDBC stands for Java Database Connectivity: It is an API which gives easy connection to a wide range of databases. To connect to a database we have to load the appropriate dri

public class Tester { static void test(int[] a) { int[] b = new int[2]; a = b; System.out.print(b.length); System.out.print(a.length); } public static void main(String[] arg

Question: (a) Explain the importance of abstract classes in Java with an appropriate example. (b) Explain what is meant by an interface in Java? Use a simple example to il

What are object/relational mapping integration module? Ans) Spring also supports for using of an object/relational mapping (ORM) tool over straight JDBC by giving the ORM module

import java.text.NumberFormat; import java.util.Locale; public class Client {    // instance data    private String name;    private long income_this_year;    private double perce


An ActionForm is a JavaBean that extends org.apache.struts.action.ActionForm.  ActionForm handles the session state for web application and the ActionForm object is automatically p