import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class EqualsHashcode {
public static void main(String... args){
final int x = 4;
Set<SortOf> set= new HashSet<SortOf>();
SortOf t = new SortOf(2,"abc", (short) x);
SortOf t1 = new SortOf(1,"abc", (short) (x + 4));
set.add(t);
set.add(t1);
ArrayList <? super SortOf> a = new ArrayList<SortOf>();
add(a);
System.out.println(set.size());
}
public static void add(List <? super SortOf> a){
a.add(new Object());
// Compile Time Error
}
}
class SortOf {
String name;
int bal;
String code;
short rate;
SortOf(int bal,String code, short rate){
this.bal = bal;
this.code = code;
this.rate = rate;
}
public int hashcode() {
return (code.length() * bal);
}
public boolean equals(Object o) {
return ((SortOf) o).code.length() * ((SortOf) o).bal
* ((SortOf) o).rate == this.code.length() * this.bal
* this.rate;
}
}
Can anyone explain this behavior? // Compile Time Error why is it coming??
[ September 19, 2007: Message edited by: Yogvinder Singh Ghotra ]