我正在尝试在ArrayList中使用contains方法,我在其中存储了类型Employee的对象.但是当我使用这个方法时,即使我在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;
}
Run Code Online (Sandbox Code Playgroud)
测试类:
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
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
199 次 |
| 最近记录: |