Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training  

hashcode() and equals()



Share on Google+Share on Google+

hashcode() and equals()
1 Answer(s)      8 years ago
Posted in : Java Beginners
View Answers

September 1, 2009 at 12:42 PM

Hi Friend,

Try the following code:

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) { = id;}
public int hashCode() {
return id;}
public boolean equals(Object o) {
return true;



Related Tutorials/Questions & Answers: