私は種類の従業員のオブジェクトを格納した ArrayList の contains メソッドを使用しようとしています。しかし、このメソッドを使用すると、Employee クラスに equals メソッドを実装しても、常に false が返されます。
これは私が試したものです:
従業員クラス:
public class Employee {
.
.
.
@Override
public boolean equals(Object o){
if(o instanceof Employee)
{
Employee e = (Employee) o;
return this.id==(e.id);
}
return false;
}
@Override
public int hashCode() {
int hash = 7;
hash = 79 * hash + (int) (this.id ^ (this.id >>> 32));
return hash;
}
テストクラス:
public class Test {
LinkedList <Employee>list=new LinkedList <Employee>();
Employee e=new Employee();
long id=0;
e.setId(20710456);
list.add(e);
e.setId(30560432);
list.add(e);
public void Edit()
{
id=Integer.parseInt(JOptionPane.showInputDialog(null,"Enter the id of the
employee"));
e.setId(id);
System.out.print(list.contains(e.getId()));
if(list.contains(e.getId())){
.
.
//rest of the code
}
}
}