Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


nagaraju
equals and hashcode
1 Answer(s)      2 years and 8 months ago
Posted in : Java Interview Questions


Could you please explain me why equals and hashcode methods to override? and which implementations should override these methods? give me an example...


Advertisement
View Answers

February 25, 2012 at 1:44 PM


In Java, every object has access to the equals() method because it is inherited from the Object class. However, this default implementation just simply compares the memory addresses of the objects. You can override the default implementation of the equals() method defined in java.lang.Object. If you override the equals(), you MUST also override hashCode(). Otherwise a violation of the general contract for Object.hashCode will occur, which can have unexpected repercussions when your class is in conjunction with all hash-based collections.

Example:

import java.util.*;
public class HashTableExample{

public static void main (String args[]){
java.util.Hashtable hashtable = new java.util.Hashtable();
hashtable.put(new Entity(1), new Entity(1));
hashtable.put(new Entity(2), new Entity(2));
hashtable.put(new Entity(3), new Entity(3));
System.out.println("Size of hashtable is: "+hashtable.size());
}
}
class Entity{
int id;
Entity(int id) {this.id = id;}
public int hashCode() {
return id;}
public boolean equals(Object o) {
return true;
}
}


Related Tutorials/Questions & Answers:
equals and hashcode
in java.lang.Object. If you override the equals(), you MUST also override hashCode...equals and hashcode  Could you please explain me why equals and hashcode methods to override? and which implementations should override these methods
Overriding equals and hashCode in Java
Overriding equals and hashCode in Java  Overriding equals and hashCode in Java
Advertisements
Equals() and Hashcode() - Java Beginners
Equals() and Hashcode()  What is Equals() and Hashcode()? what... Equals and HashCode() are the methods of Object class. The Equals() method returns.... Thanks   can you give one hashtable prog using equals() and hashcode
hashcode() and equals() - Java Beginners
hashcode() and equals()  plesae give an example of hashtable using equals() and hashcode()?  Hi Friend, Try the following code...) {this.id = id;} public int hashCode() { return id;} public boolean equals
hashcode() and equals() - Java Interview Questions
hashcode() and equals()  can you give one hashtable prog using equals() and hashcode()?PLease..thanks in advance
Equals() and Hashcode() - Java Interview Questions
Equals() and Hashcode()  What is Equals() and Hashcode()? what is the importance and where we use these methods? why and where we have to override... of hashtable using equals() and hashcode
why should we over ride hashCode(), when equals was over ride()?
why should we over ride hashCode(), when equals was over ride()?  why should we over ride hashCode(), when equals was over ride
Equals()
Equals()  What is role of equals() method in camparator interface
how to override the hashCode() method ? and What is the advantage of overide the hashcode().
how to override the hashCode() method ? and What is the advantage of overide the hashcode().   send me code   It is not always necessary to override hashcode and equals. But if you think you need to override one
HashMap and HashCode
.style1 { color: #FFFFFF; } HashMap and HashCode The HashMap... implementation of the method hashCode( ). HashCode is a method that returns... HashMaps and HashSets. The objects are only equal if if their hashCode
difference between hashcode,reference in java
difference between hashcode,reference in java  difference between hashcode,reference in java
1024 bytes equals to
1024 bytes equals to  1024 bytes equals to   1024 bytes equals to 1 KB
Java file hashCode
Java file hashCode This section demonstrates you the use of method hashCode(). Explanation: A hashCode is a way of computing a small (32-bit) digest numeric key from a String or even an arbitrary clump of bytes. The method hashCode
difference b/w == and equals()
difference b/w == and equals()  what is the difference between == operator and equals()   Hi Friend, The == operator checks if the two... address whereas the equals() method is used to compare the values of the object
objective c if string equals
objective c if string equals   Checking if a string is equal to ' " '   In objective c, isEqualToString is a keyword that allows you to check if the string is equal to or not. if ([text isEqualToString
Constructors .equals method
Constructors .equals method  Hi, my two dates never match using .equals method and two different constructors. o/p is like examples.Pdate@192d342Blah dateexamples.Pdate@6b97fd Code: package examples; public class Pdate
Constructors .equals method
Constructors .equals method  Hi, my two dates never match using .equals method and two different constructors. o/p is like examples.Pdate@192d342Blah dateexamples.Pdate@6b97fd Code: package examples; public class Pdate
CodeRelief
CodeRelief The CodeRelief Eclipse plugin generates the code for the equals(), hashCode() & toString() methods. All you do is select the class variables you want to include & VOILA, you have the code for the method. All
ShortBuffer in java, Use of hashCode() in short buffer.
ShortBuffer in java, Use of hashCode() in short buffer. In this tutorial, we will see how to use of hashCode() in short buffer. ShortBuffer API....  final int hashCode() The hashCode(..) method returns
Difference between equals() and == ?
Difference between equals() and == ? In this section you will find the difference between equals() and ==. When we create a object using new keyword... equals() and "= =" are used to compare object to check equality
AutoBoxing,==,!=,equals() - Java Interview Questions
AutoBoxing,==,!=,equals()  Hi, I am really confused of ==,!=,equals() operations on Autoboxing the new feature in java 5.0 . Can any one tell me,how the above operations are performed on Boxing? See the below examples
Use of hashCode() method of LongBuffer class in java.
Use of hashCode() method of LongBuffer class in java. In this tutorial, we will see the use of hashCode() method of LongBuffer class in java...; int hashCode() The hashCode() method returns current has code
Java file equals
Java file equals This section illustrates you the use of method equals... file class objects using equals() method. This method checks whether... of both the files are different. No matter of having the same file name. equals
Java BigDecimal hashCode example
Java BigDecimal hashCode example       In this example bigdecimal class hashCode() method working... value.  Syntax for using the method: public int hashCode() System.out.println
equals method in java
Description: The equals method found in java.lang.object. It is use to check  state(data) of object not identifying the location in Memory of the object. It return the Boolean value. In other way it compare content of 
Difference between == and equals method in java
Description: For comparing equality of string ,We Use equals() Method... "equals()" method .  "==" compares the reference value of string object whereas equals() method is present in the java.lang.Object
Case-insensitive equals using Hibernate Criteria.
Case-insensitive equals using Hibernate Criteria.  How to use Case-insensitive equals with Hibernate Criteria?   The Case Insensitive ignores the case of the latter. It Selects all the records of the table related
Case-sensitive equals using Hibernate Criteria.
Case-sensitive equals using Hibernate Criteria.  How to check for case sensitive equals in Hibernate criteria?   package net.roseindia.main; import java.util.*; import net.roseindia.table.Employee; import
Use of equals() method of ByteBuffer class in java.
Use of equals() method of ByteBuffer class in java.  In this tutorial, we...; The allocate(..)method allocate a new byte buffer. boolean equals(object obj) The equals() method tells this buffer is equals or not to another
Use of equals() method of  FloatBuffer class in java.
Use of equals() method of  FloatBuffer class in java.  ... equals(object obj) The equals() method tells this buffer is equals...; System.out.println("Equals");     } 
Advertisements
 

 

 

DMCA.com