Difference hashcode and equal, JAVA Programming

Assignment Help:

 

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.

 


Related Discussions:- Difference hashcode and equal

Create sudoku in java, The SudCell Class A single cell in a Sudoku ...

The SudCell Class A single cell in a Sudoku must clearly represent a number from 1 to 9 .  But in a solver context, we need much more.  It isn't enough to say "this c

Give an examples of access protection, Give an Examples of Access Protectio...

Give an Examples of Access Protection ? This is how the Car class would almost certainly be written in practice. Remember that all the fields are now declared private, and the

Ejb container security?, EJB elements operate inside a container environmen...

EJB elements operate inside a container environment and rely heavily on the container to give security. The four key services needed for the security are: 1. Identification :

How to draw polygons in java, How to draw Polygons in java? In Java rec...

How to draw Polygons in java? In Java rectangles are defined through the position of their upper left hand corner, their height, and their width. Therefore it is implicitly sup

Functions in javascript, The following set of short questions will focus on...

The following set of short questions will focus on getting you familiar with JavaScript functions. All functions and testing should appear in a single XHTML 1.0 Strict page. All

How can you define a readable program, How can you define a readable progra...

How can you define a readable program? A program that is easy to read & understand also easy to maintain and enhance. Readability is the ease in that text can be read and under

What are the kinds of computer network among nodes, What are the kinds of c...

What are the kinds of computer network according to the distance among nodes? the kinds of computer network according to the distance among nodes are as follows: There are t

Program for extend a java based database management system, Create a compre...

Create a compressed file that contains the eclipse project directory and a short document that describes:   the methods implemented any missing or incomplete elements o

Write code in javascript language to show the odd numbers, Write code in Ja...

Write code in JavaScript language to show the odd numbers among 20 to 100 using FOR statement. Note: No requirement to write whole HTML program. Just JavaScript code of need p

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd