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



Home Answers Viewqa Java-Beginners hashcode() and equals()
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


venkatesh
hashcode() and equals()
1 Answer(s)      5 years and 2 months ago
Posted in : Java Beginners



Advertisement
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:
 

 

 

DMCA.com