hashcode() and equals()
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) {this.id = id;}
public int hashCode() {
return id;}
public boolean equals(Object o) {
return true;
}
}
Thanks
Related Tutorials/Questions & Answers: