why it doesn't work?
That's my program, i want to see whether hashCode() and equals() would be involked when Vector.contains() (or Vector.indexOf()) is used. But it seems that these two fonctions don't be involked by contains(). What's the problem?
import java.lang.*;
import java.util.*;
import classes.*;
class testterm{
String s;
int i;
testterm(String ss, int ii){
s = ss;
i = ii;
}
public int hashCode(){
P.rintln("in the hashCode fonction");
return s.hashCode();
}
public boolean equals(testterm tm){
P.rintln("in the equals fonction");
return tm.s.equals(this.s);
}
public String toString(){
return new String(s+"-"+i);
}
}
public class testcomparable{
public static void main(String[] args){
Vector v = new Vector();
v.add(new testterm("key1", 2));
v.add(new testterm("key1", 3));
testterm tt = new testterm("key1", 3);
P.rintln("Before contains fonction");
if(v.contains(tt))
P.rintln("yes");
P.rintln("After contains fonction");
P.rintln(((testterm)v.elementAt(0)).toString()+" hashCode:"+((testterm)v.elementAt(0)).hashCode());
P.rintln(((testterm)v.elementAt(1)).toString()+" hashCode:"+((testterm)v.elementAt(1)).hashCode());
P.rintln(tt.toString()+" hashCode:"+tt.hashCode());
}
}
G:\JavaCodes>java testcomparable
Before contains fonction
After contains fonction
in the hashCode fonction
key1-2 hashCode:3288498
in the hashCode fonction
key1-3 hashCode:3288498
in the hashCode fonction
key1-3 hashCode:3288498

